Table of Contents

Method Choose

Namespace
SodaFlow.Functional
Assembly
SodaFlow.Functional.dll

Choose<T, TResult>(IEnumerable<T>?, Func<T, Maybe<TResult>>)

Applies a function which can give no value to each element of a sequence, and gives the results with a value.

[Pure]
public static IEnumerable<TResult> Choose<T, TResult>(this IEnumerable<T>? source, Func<T, Maybe<TResult>> selector)

Parameters

source IEnumerable<T>

The sequence to map. A null sequence counts as empty.

selector Func<T, Maybe<TResult>>

Applied to each element in turn.

Returns

IEnumerable<TResult>

The results for which selector produced a value, in order, with the elements it produced nothing for left out.

Type Parameters

T

The type of the values in the sequence.

TResult

The type of the values the function produces.

Remarks

Filters and maps in one step. Use it for the usual condition where a test of an element is the same work as the value to keep. Examples are a parse, a lookup, and a narrower type. With LINQ alone that means the work two times, or a map to a Maybe<T> with an unwrap after it. This does that for you. Lazy: this code does not touch the source or selector until the result goes through an enumeration. Use AllSomeOrNone<T, TResult>(IEnumerable<T>?, Func<T, Maybe<TResult>>) where one element that gives nothing must fail the full result.

Choose<T, TResult>(IEnumerable<T>?, Func<T, int, Maybe<TResult>>)

Applies a function which can give no value to each element of a sequence along with its index, and gives the results with a value.

[Pure]
public static IEnumerable<TResult> Choose<T, TResult>(this IEnumerable<T>? source, Func<T, int, Maybe<TResult>> selector)

Parameters

source IEnumerable<T>

The sequence to map. A null sequence counts as empty.

selector Func<T, int, Maybe<TResult>>

Applied to each element in turn, along with the index of that element in the source.

Returns

IEnumerable<TResult>

The results for which selector produced a value, in order, with the elements it produced nothing for left out.

Type Parameters

T

The type of the values in the sequence.

TResult

The type of the values the function produces.

Remarks

The index is the position in the source, thus it counts the elements that gave nothing.