selectel/mongoDB-haskell — pull request #9 by lucasdicioccio (2013-05-09 13:17:54+00:00)
Title: function to apply

Correct me if I'm wrong :).

Current API allows to use some_cursor >>= 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