Seq
Source: lib/Seq.cs
This page is generated from the source by scripts/gen-reference.py.
Seq: LINQ-style query operators (Where, Select, Sum, Count, ...) over spans, built so that a query allocates nothing while it runs.
.NET's LINQ allocates an iterator object per operator and a closure per capturing lambda. Without a garbage collector that memory is never given back, so a query in a loop would grow the program without limit. Here:
Each operator returns a ref struct that wraps the one before it, so a whole query is one value on the stack. Nothing is allocated to build it or to run it, and
foreachruns it directly.Lambdas that capture nothing are allocated once, the first time they run, and then reused by the compiler. Mark them
staticso a capture by mistake is a compile error.A lambda that needs a value from outside takes it as a state argument instead of capturing it: Where(min, static (min, x) => x > min).
Queries start from a span (arrays included), a Listfrom x in numbers where x > 2 select x. ToList(arena) is the way to keep results.
GroupBy has to collect every item before it can answer, so it takes an arena, like ToList, and builds the groups there: a list of (key, items) pairs, in the order the keys first appear. It's freed with the arena. Keys are compared by their bytes, as in Dict. Query syntax's group ... by can't pass an arena, so call GroupBy as a method.
A query reads its span as it runs, so the span must stay valid until it ends, and a query can't be stored in a field (it's a ref struct).
Starting points for queries, and the operators that need a particular item type.
Members
AsQuery
public static Query<T, SpanSource<T>> AsQuery<T>(this ReadOnlySpan<T> span)
A query over span.
Where
public static Query<T, WhereSource<T, SpanSource<T>>> Where<T>(
this ReadOnlySpan<T> span,
Func<T, bool> predicate)
The items of span that predicate accepts.
TState>
public static Query<T, WhereSource<T, TState, SpanSource<T>>> Where<T, TState>(
this ReadOnlySpan<T> span,
TState state,
Func<TState, T, bool> predicate)
The items of span that predicate accepts, with state passed to it.
TResult>
public static Query<TResult, SelectSource<T, TResult, SpanSource<T>>> Select<T, TResult>(
this ReadOnlySpan<T> span,
Func<T, TResult> selector)
Each item of span turned into another by selector.
public static Query<TResult, SelectSource<T, TResult, TState, SpanSource<T>>> Select<T, TState, TResult>(
this ReadOnlySpan<T> span,
TState state,
Func<TState, T, TResult> selector)
Each item of span turned into another by selector, with state passed to it.
AsQuery
public static Query<T, ListSource<T>> AsQuery<T>(
this List<T> list) where T : unmanaged
A query over the items of list, read with Get, so adding to the list while the query runs is safe.
Range
public static Query<int, RangeSource> Range(int start, int count)
The count numbers from start up.
Sum
public static int Sum<TSource>(
this Query<int, TSource> query) where TSource : ISeqSource<int>, allows ref struct
The sum of the items.
public static long Sum<TSource>(
this Query<long, TSource> query) where TSource : ISeqSource<long>, allows ref struct
The sum of the items.
public static double Sum<TSource>(
this Query<double, TSource> query) where TSource : ISeqSource<double>, allows ref struct
The sum of the items.
TSource>
public static List<Group<TKey, T>> GroupBy<T, TKey, TSource>(
this Query<T, TSource> query,
Arena arena,
Func<T, TKey> keySelector)
The items grouped by the key keySelector gives each, as a list in arena with one Group{TKey, T} per key, in the order the keys first appear. A not-created list if the arena is out of memory.
TKey>
public static List<Group<TKey, T>> GroupBy<T, TKey>(
this ReadOnlySpan<T> span,
Arena arena,
Func<T, TKey> keySelector)
The items of span grouped by key; see the other GroupBy.
TSource>
public static List<T> ToList<T, TSource>(
this Query<T, TSource> query,
Arena arena) where T : unmanaged where TSource : ISeqSource<T>, allows ref struct
The items in a new list in arena (a not-created list if it is out of memory).
Query
A query: a chain of operators over a source. Build one with Seq.
Where
public Query<T, WhereSource<T, TSource>> Where(Func<T, bool> predicate)
The items predicate accepts.
public Query<T, WhereSource<T, TState, TSource>> Where<TState>(
TState state,
Func<TState, T, bool> predicate)
The items predicate accepts, with state passed to it instead of captured.
Select
public Query<TResult, SelectSource<T, TResult, TSource>> Select<TResult>(
Func<T, TResult> selector)
Each item turned into another by selector.
TResult>
public Query<TResult, SelectSource<T, TResult, TState, TSource>> Select<TState, TResult>(
TState state,
Func<TState, T, TResult> selector)
Each item turned into another by selector, with state passed to it instead of captured.
Take
public Query<T, TakeSource<T, TSource>> Take(int count)
The first count items.
Skip
public Query<T, SkipSource<T, TSource>> Skip(int count)
All but the first count items.
Count
public int Count()
The number of items.
Any
public bool Any()
Whether there is at least one item.
public bool Any(Func<T, bool> predicate)
Whether predicate accepts at least one item.
All
public bool All(Func<T, bool> predicate)
Whether predicate accepts every item (true when there are none).
TryFirst
public bool TryFirst(out T item)
The first item, if there is one.
Aggregate
public TAccumulate Aggregate<TAccumulate>(
TAccumulate seed,
Func<TAccumulate, T, TAccumulate> combine)
Combines the items, starting from seed: Aggregate(0, static (sum, x) => sum + x).
GetEnumerator
public Query<T, TSource> GetEnumerator()
Runs the query in a foreach loop.
MoveNext
public bool MoveNext()
For foreach.
Current
public T Current
For foreach.