Compact - Kalkwst/Risotto GitHub Wiki
Remove all "falsy" values from the source sequence.
In this page
Overload | Description |
---|---|
Compact() | Remove all "falsy" values from the source sequence. |
Compact(Func<TSource, bool> fn) | Remove all elements that are considered "falsy" by the provided predicate. |
Remove all "falsy" values from the source sequence.
public static IEnumerable<TSource> Compact<TSource>(this IEnumerable<TSource> source);
TSource
The type of the elements of source
.
source
IEnumerable <TSource>
A sequence that contains "falsy" values that need to be removed.
IEnumerable<IEnumerable<TSource>>
A sequence containing all of the elements that are not considered "falsy".
ArgumentNullException
source
is null
The following code example demonstrates how to use Compact() to remove all "falsy" values from an IEnumerable.
IEnumerable<int> source = new int[] { 1, 2, 3, 0, 0, 0, 0, 0 };
source.Compact()
//=> { 1, 2, 3 }
Remove all elements that are considered "falsy" by the provided predicate.
public static IEnumerable<TSource> Compact<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> fn);
TSource
The type of the elements of source
.
source
IEnumerable <TSource>
A sequence that contains the elements to be compacted by removing "falsy" values.
fn
Func <IEnumerable<TSource>, bool>
The filtering function
IEnumerable<TSource>
A sequence of filtered values.
ArgumentNullException
source
is null
-or-
fn
is null
The following code example demonstrates how to use Compact(Func<TSource, bool> fn) to remove all "falsy" values from an IEnumerable, based on the provided filtering function.
IEnumerable<int> source = new int[] { 1, 2, 3, 4, 5, 5, 5 };
source.Compact(x => x % 2 == 0);
//=> { 2, 4 }