Method Segment
| Edit this page View SourceSegment<T>(IEnumerable<T>, Func<T, bool>)
Divides a sequence into multiple sequences by using a segment detector based on the original sequence
Declaration
public static IEnumerable<IReadOnlyList<T>> Segment<T>(this IEnumerable<T> source, Func<T, bool> newSegmentPredicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence to segment |
Func<T, bool> | newSegmentPredicate | A function, which returns true if the given element begins a new segment, and false otherwise |
Returns
Type | Description |
---|---|
IEnumerable<IReadOnlyList<T>> | A sequence of segment, each of which is a portion of the original sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example demonstrates how to split a sequence based on a segment detector using Segment
.
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
(key: "feb", value: 987),
(key: "Feb", value: 654),
(key: "FEB", value: 321),
(key: "mar", value: 789),
(key: "Mar", value: 456),
(key: "MAR", value: 123),
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 781),
};
// Group adjacent items
var result = sequence
.Segment(
x => x.key[0] == 'm');
Console.WriteLine(
"[" + Environment.NewLine +
string.Join(
", " + Environment.NewLine,
result.Select(c => " [" + string.Join(", ", c) + "]")) +
Environment.NewLine + "]");
// This code produces the following output:
// [
// [(jan, 123), (Jan, 456), (JAN, 789), (feb, 987), (Feb, 654), (FEB, 321)],
// [(mar, 789), (Mar, 456), (MAR, 123), (jan, 123), (Jan, 456), (JAN, 781)]
// ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Segment<T>(IEnumerable<T>, Func<T, int, bool>)
Divides a sequence into multiple sequences by using a segment detector based on the original sequence
Declaration
public static IEnumerable<IReadOnlyList<T>> Segment<T>(this IEnumerable<T> source, Func<T, int, bool> newSegmentPredicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence to segment |
Func<T, int, bool> | newSegmentPredicate | A function, which returns true if the given element and index begins a new segment, and false otherwise |
Returns
Type | Description |
---|---|
IEnumerable<IReadOnlyList<T>> | A sequence of segment, each of which is a portion of the original sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example demonstrates how to split a sequence based on a segment detector using Segment
.
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
(key: "feb", value: 987),
(key: "Feb", value: 654),
(key: "FEB", value: 321),
(key: "mar", value: 789),
(key: "Mar", value: 456),
(key: "MAR", value: 123),
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 781),
};
// Group adjacent items
var result = sequence
.Segment(
(x, i) => i % 3 == 0);
Console.WriteLine(
"[" + Environment.NewLine +
string.Join(
", " + Environment.NewLine,
result.Select(c => " [" + string.Join(", ", c) + "]")) +
Environment.NewLine + "]");
// This code produces the following output:
// [
// [(jan, 123), (Jan, 456), (JAN, 789)],
// [(feb, 987), (Feb, 654), (FEB, 321)],
// [(mar, 789), (Mar, 456), (MAR, 123)],
// [(jan, 123), (Jan, 456), (JAN, 781)]
// ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Segment<T>(IEnumerable<T>, Func<T, T, int, bool>)
Divides a sequence into multiple sequences by using a segment detector based on the original sequence
Declaration
public static IEnumerable<IReadOnlyList<T>> Segment<T>(this IEnumerable<T> source, Func<T, T, int, bool> newSegmentPredicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence to segment |
Func<T, T, int, bool> | newSegmentPredicate | A function, which returns true if the given current element, previous element, and index begins a new segment, and false otherwise |
Returns
Type | Description |
---|---|
IEnumerable<IReadOnlyList<T>> | A sequence of segment, each of which is a portion of the original sequence |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence |
Remarks
This method is implemented by using deferred execution and streams the groupings. The grouping elements, however, are buffered. Each grouping is therefore yielded as soon as it is complete and before the next grouping occurs.
Examples
The following code example demonstrates how to split a sequence based on a segment detector using Segment
.
var sequence = new[]
{
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 789),
(key: "feb", value: 987),
(key: "Feb", value: 654),
(key: "FEB", value: 321),
(key: "mar", value: 789),
(key: "Mar", value: 456),
(key: "MAR", value: 123),
(key: "jan", value: 123),
(key: "Jan", value: 456),
(key: "JAN", value: 781),
};
// Group adjacent items
var result = sequence
.Segment(
(cur, prev, i) => !string.Equals(cur.key[..1], prev.key[..1], StringComparison.OrdinalIgnoreCase));
Console.WriteLine(
"[" + Environment.NewLine +
string.Join(
", " + Environment.NewLine,
result.Select(c => " [" + string.Join(", ", c) + "]")) +
Environment.NewLine + "]");
// This code produces the following output:
// [
// [(jan, 123), (Jan, 456), (JAN, 789)],
// [(feb, 987), (Feb, 654), (FEB, 321)],
// [(mar, 789), (Mar, 456), (MAR, 123)],
// [(jan, 123), (Jan, 456), (JAN, 781)]
// ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|