Method Window
View SourceWindow<TSource, TResult>(IEnumerable<TSource>, int, ReadOnlySpanFunc<TSource, TResult>)
Processes a sequence into a series of subsequences representing a windowed subset of the original, and then projecting them into a new form.
Declaration
public static IEnumerable<TResult> Window<TSource, TResult>(this IEnumerable<TSource> source, int size, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to evaluate a sliding window over. |
int | size | The size (number of elements) in each window. |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | selector | A transform function to apply to each window. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | An IEnumerable<T> whose elements are the result of invoking the transform function on each
window of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A window can contain fewer elements than size
, especially as it slides over the start of
the sequence.
In this overload of Window
, a single array of length size
is allocated as a
buffer for all subsequences.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Window<TSource, TResult>(IEnumerable<TSource>, TSource[], ReadOnlySpanFunc<TSource, TResult>)
Processes a sequence into a series of subsequences representing a windowed subset of the original, and then projecting them into a new form.
Declaration
public static IEnumerable<TResult> Window<TSource, TResult>(this IEnumerable<TSource> source, TSource[] array, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to evaluate a sliding window over. |
TSource[] | array | An array to use as a buffer for each subsequence. |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | selector | A transform function to apply to each window. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | An IEnumerable<T> whose elements are the result of invoking the transform function on each
window of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A window can contain fewer elements than
, especially as it slides
over the start of the sequence.
array
.Length
In this overload of Window
, array
is used as a common buffer for all
subsequences.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Window<TSource, TResult>(IEnumerable<TSource>, TSource[], int, ReadOnlySpanFunc<TSource, TResult>)
Processes a sequence into a series of subsequences representing a windowed subset of the original, and then projecting them into a new form.
Declaration
public static IEnumerable<TResult> Window<TSource, TResult>(this IEnumerable<TSource> source, TSource[] array, int size, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to evaluate a sliding window over. |
TSource[] | array | An array to use as a buffer for each subsequence. |
int | size | The size (number of elements) in each window. |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | selector | A transform function to apply to each window. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | An IEnumerable<T> whose elements are the result of invoking the transform function on each
window of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A window can contain fewer elements than size
, especially as it slides over the start of
the sequence.
In this overload of Window
, array
is used as a common buffer for all
subsequences.
This overload is provided to ease usage of common buffers, such as those rented from ArrayPool<T>, which may return an array larger than requested.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Window<TSource>(IEnumerable<TSource>, int)
Processes a sequence into a series of subsequences representing a windowed subset of the original
Declaration
public static IEnumerable<IList<TSource>> Window<TSource>(this IEnumerable<TSource> source, int size)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The sequence to evaluate a sliding window over |
int | size | The size (number of elements) in each window |
Returns
Type | Description |
---|---|
IEnumerable<IList<TSource>> | A series of sequences representing each sliding window subsequence |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the source sequence |
Remarks
The number of sequences returned is: Max(0,
source
.Count() - size
+
1)
Returned subsequences are buffered, but the overall operation is streamed.
Examples
The following code example demonstrates how to generate a sliding window over a sequence using Window
.
var sequence = Enumerable.Range(1, 10);
// Get a sliding window over the sequence
var result = sequence.Window(3);
Console.WriteLine(
"[" + Environment.NewLine +
string.Join(
", " + Environment.NewLine,
result.Select(c => " [" + string.Join(", ", c) + "]")) +
Environment.NewLine + "]");
// This code produces the following output:
// [
// [1, 2, 3],
// [2, 3, 4],
// [3, 4, 5],
// [4, 5, 6],
// [5, 6, 7],
// [6, 7, 8],
// [7, 8, 9],
// [8, 9, 10]
// ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|