-
Notifications
You must be signed in to change notification settings - Fork 416
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
Aggregate all or some/partial/none #899
Comments
It could've been simpler with a single discriminated union type: TResult Aggregate<TElement, TState, TResult>(
this IEnumerable<TElement> source,
TState seed,
Func<TState, TSecond, FoldResult<TState, TResult>> folder,
Func<TState, TResult> selector) where FoldResult<,> is either Accept(TState) or Break(TResult) input.Tokens.Aggregate(
ImmutableArray<int>.Empty,
s => int.TryParse(s, out var n) ? FoldResult.Accept(a.Add(n)) : FoldResult.Break(default(ImmutableArray<int>)),
a => a) is { IsDefault: false } ys) The only question is how to implement that type. It should be a structure + a static class with smart ctors for the two options + a couple of structures for Accept and Break cases implicitly converted to FoldResult<,>... Unless there will be just a version without final TState->TResult projection and without TResult type param at all |
@declard That's true but the approach taken so far in MoreLINQ is what I like to call bring your own types (BYOT). This can complicate signatures a bit, but it gives the caller full control and also alleviates the burden of introducing and maintaining new types. If you squint, then |
I'd like to propose adding an overload of
Aggregate
that will use a function to determine the validity of each item in the source sequence. As long as items are valid, they will be accumulated and a function will be called at the end to turn the accumulator into a result. As soon as one item is invalid, the iteration of the source sequence will be halted and a function will be called to turn the partial accumulation into a result.This enables one to implement the following strategies:
A prototype of such an extension would be as follows:
This is like
Choose
+Aggregate
rolled into one, but which cannot be done otherwise by combining the two without considerable and additional effort. The following example shows the above in action, together with how it differs fromChoose
:The output of the above example will be as follows:
The text was updated successfully, but these errors were encountered: