function to apply #9
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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