Compact - Kalkwst/Risotto GitHub Wiki

Definition

Remove all "falsy" values from the source sequence.

In this page

Overloads

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.

Compact()

Remove all "falsy" values from the source sequence.

public static IEnumerable<TSource> Compact<TSource>(this IEnumerable<TSource> source);

Type Parameters

TSource
The type of the elements of source.

Parameters

source IEnumerable <TSource>
A sequence that contains "falsy" values that need to be removed.

Returns

IEnumerable<IEnumerable<TSource>>
A sequence containing all of the elements that are not considered "falsy".

Exceptions

ArgumentNullException
source is null

Example

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 }

Compact(Func<TSource, bool> fn)

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);

Type Parameters

TSource
The type of the elements of source.

Parameters

source IEnumerable <TSource>
A sequence that contains the elements to be compacted by removing "falsy" values.

fn Func <IEnumerable<TSource>, bool>
The filtering function

Returns

IEnumerable<TSource>
A sequence of filtered values.

Exceptions

ArgumentNullException
source is null

-or-

fn is null

Example

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 }
⚠️ **GitHub.com Fallback** ⚠️