-
Notifications
You must be signed in to change notification settings - Fork 418
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
Spill head projection to remaining sequence #752
Comments
Based on work so far in PR #753, I feel it's best to rename the extension to |
Is it essentially this, but working in constant space? public static IEnumerable<R>
SpillSpan<T, P, R>(
this IEnumerable<T> source,
Func<T, bool> predicate,
Func<T, P> prefixMapping,
Func<P, T, R> resultSelector)
{
var (prefix, rest) = source.Span(predicate);
var mappedPrefix = prefixMapping(prefix);
return rest.Select(e => resultSelector(mappedPrefix, e));
}
// applied to a predicate `predicate` and an enumerable `source`, returns a tuple where first element is longest prefix (possibly empty) of `source` of elements that satisfy `predicate` and second element is the remainder of the enumerable
// >>> span (< 3) [1,2,3,4,1,2,3,4]
// ([1,2],[3,4,1,2,3,4])
public static (IReadOnlyCollection<T> Prefix, IReadOnlyCollection<T> Rest) Span<T>(this IEnumerable<T> source, Func<T, bool> predicate); The |
Not quite like |
I propose to add an extension that takes one or more elements at the head of a sequence and spills a projection to remaining elements of the sequence.
Sometimes you have a sequence where the initial element(s) contains information about the processing of the rest of the sequence. A typical example is a table (think CSV) where the table is composed of rows; where the first row is a header and the remaining the data rows. Processing such a sequence should only generate a projection of the data rows.
The signature would be as follows:
The
chooser
identifies header elements. Data rows commence as soon as it returns(false, _)
for aT
. The header elements are accumulated viaaccumulator
and theseeder
is used to seed the accumulation with the initial header element. Theempty
value is used for the headless case. TheheaderSelector
function is used to create a single projection out of the accumulated header elements and which is subsequently paired with or spilled to remaining elements.I propose to add overloads for simpler cases.
SpillSpan
should never throw an exception. If the user wants to ban the headless case, he/she can throw inheaderSelector
upon receiving theempty
value.Example
Output:
The text was updated successfully, but these errors were encountered: