feat: add scan combinator for iterators #1590
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.
This PR depends on:
This PR was originally #1585, but I split the PR it was based on into multiple PRs and wanted to avoid merge conflicts.
This PR builds off of my earlier PRs and adds a Scan combinator to Iterators as well (the dependence on the earlier PRs is just for the lemmas that prove correspondence between the iterator combinators and their list/array equivalents).
The primary contribution of this PR is IterM.scanM which, given an effectful iterator, an inital value, and an effectful accumulation function, returns an iterator over the partial results of folding that function over the values of the inner iterator. The remaining Iter.Scan/Iter.ScanM/IterM.scan are just implemented in terms of this in the obvious way.
This PR includes instances for both Productive and Finite (if the underlying iterator is Productive, resp. Finite) and includes lemmas showing that (at least for pure iterators / iterators in the identity monad) Iter.scan* is equivalent to accumulating the values into a list (resp. Array) and then calling List.scanl/List.scanlM (resp. Array.scanl/Array.scanlM). I would be happy to continue plugging away at more theorems, but this seems like a minimally useful set.