How to use mongoDB bindings for haskell

I am new to Haskell, but I feel that I have a decent understanding of everyone.

I'm currently trying to play with 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 method returns / resolves the IO connection .

However, all methods that actually intercept the database take just Connection as an argument . Example:

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

I think there is something fundamental, I don’t understand here, maybe IO has to do with the fact that it is part of IO Monad? I am really quite ignorant and I was wondering if anyone has any light to shed it on me.

How can I persuade an IO connection to a connection in mongoDB bindings?

Thanks for any input you have.

+3
source share
1 answer

I think that there is something fundamental, I do not understand here.

Yes, it's right. You are just missing out on how Haskell distinguishes code that has side effects from pure code. To use code that ends with an I / O type, you use a service record. For instance.

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

< - "", . "".

Haskell IO, . Real World Haskell, http://book.realworldhaskell.org/read/io.html

, http://hackage.haskell.org/packages/archive/mongoDB/0.2/doc/html/Database-MongoDB.html

+10

Source: https://habr.com/ru/post/1734705/


All Articles