开发者

Howto use mongoDB bindings for haskell

开发者 https://www.devze.com 2022-12-21 22:34 出处:网络
I am pretty new to Haskell but开发者_StackOverflow I feel like I have a decent understanding over all.

I am pretty new to Haskell but开发者_StackOverflow I feel like I have a decent understanding over all.

I'm currently trying to play with the unofficial mongoDB bindings for haskell.

If you look at the code here: http://github.com/srp/mongoDB/blob/master/Database/MongoDB.hs

connect :: HostName -> [ConnectOpt] -> IO Connection
connect = flip connectOnPort (Network.PortNumber 27017)

As you can see this methods returns/resolves to an IO Connection.

However all methods that actually interract with the database take a just plain Connection as an argument. Ex:

disconnect :: Connection -> IO ()
disconnect = conClose

I think there is something fundamental I'm not understanding here, maybe the IO has to do with it being part of the IO Monad? I'm really pretty clueless and was wondering if anyone has any light to shed on this for me.

How can I coax a IO Connection to a Connection in the mongoDB bindings?

Thanks for any input you may have.


I think there is something fundamental I'm not understanding here

Yes, that's right. You're just missing how Haskell distinguishes code which has side effects from pure code. To use code that ends in an IO type, you use the do-notation. E.g.

main = do
   c <- connect "myhost" []
   print "connected!"
   disconnect c

The <- is a "bind" which runs the side effecting code, and returns the result. In this case, a value of type "Connection".

Read up on Haskell IO in e.g. Real World Haskell, http://book.realworldhaskell.org/read/io.html

Make sure to read the haddocks too, http://hackage.haskell.org/packages/archive/mongoDB/0.2/doc/html/Database-MongoDB.html

0

精彩评论

暂无评论...
验证码 换一张
取 消