selectel / mongoDB-haskell

MongoDB driver for Haskell
http://hackage.haskell.org/package/mongoDB
Apache License 2.0
21 stars 11 forks source link

function to apply #9

Open lucasdicioccio opened 11 years ago

lucasdicioccio commented 11 years ago

Correct me if I'm wrong :).

Current API allows to use somecursor >>= rest to get a full list of documents. If we want to run some action on every document, we can then use rest >>= mapM f . A drawback of this approach is that rest must read all documents before returning. Indeed, rest must handle the case where there is an error before finishing to read all documents from the cursor. My understanding is that given the types prevent rest to return both an error and a list of documents read before the error occured.

This behavior leaves a gap to apply a function as soon as the documents get read from the cursor (e.g., when the query returns a large number of documents and we apply a streaming algorithm on the documents). I think there should be a iterateCursor function inside the mongoDB package to handle this use case. This is my pull request. The function takes a cursor, an initial state, and a function in the Action monad that takes a document, a state and return an updated state.

My intuition is that there may be a way to write an instance for Traversable Cursor (I'm not yet strong enough in Haskell to realize whether it is useful/possible).

Cheers <3