Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TaskSeq.fold and TaskSeq.foldAsync functions #18

Merged
merged 2 commits into from
Oct 13, 2022

Conversation

abelbraaksma
Copy link
Member

@abelbraaksma abelbraaksma commented Oct 13, 2022

This adds the following two functions and a few tests, which should greatly simplify accumulating a value throughout an IAsyncEnumerable.

I should probably emphasize already that I'm not planning to have a TaskSeq.foldBack and TaskSeq.foldBackAsync, as it would require walking the full async sequence, caching it, and then reverse-accumulating, which defeats the purpose of a delay-executed asynchronous sequence. If you have that use-case, you can use TaskSeq.toArrayAsync >> Task.map (Array.foldBack yourFolderFunction yourState).

module TaskSeq =
    /// Applies a function to each element of the task sequence, threading an accumulator argument through the computation.
    val fold: folder: ('State -> 'T -> 'State) -> state: 'State -> taskSeq: IAsyncEnumerable<'T> -> Task<'State>

    /// Applies an async function to each element of the task sequence, threading an accumulator argument through the computation.
    val foldAsync: folder: ('State -> 'T -> #Task<'State>) -> state: 'State -> taskSeq: IAsyncEnumerable<'T> -> Task<'State>

@abelbraaksma abelbraaksma merged commit a8cf04d into main Oct 13, 2022
@abelbraaksma abelbraaksma deleted the add-fold-function branch October 13, 2022 14:03
@abelbraaksma abelbraaksma added topic: surface area Adds functions to the public surface area enhancement New feature or request labels Nov 4, 2022
@abelbraaksma abelbraaksma added this to the v0.1.0 milestone Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request topic: surface area Adds functions to the public surface area
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant