TryPick

Applies the given chooser function to successive elements, returning the first result of an option value that IsSome.

Returns an Option<T> that IsNone if no such element exists.

Similar to Pick, but in this case the value still in an option context and this function doesn't raises any exception.

Parameters

Returns

Func<T, Option<T2>> chooser

IEnumerable<T> source

T2

Usage

Getting the double of first odd number in a collection

//IEnumerable<int> source = { 2, 2, 4, 4, 6, 6, 7, 8, 9 }

Option<int> result = source.TryPick(value => 
            {
                if(value % 2 == 1)
                    return value * 2;
                else
                    return Option<int>.None();
            });

//result.IsSome = true
//result.Some = 14

When there is no element that satisfies the condition

//IEnumerable<int> source = { 2, 2, 4, 4, 6, 6, 6, 8, 8 }

Option<int> result = source.TryPick(value => 
            {
                if(value % 2 == 1)
                    return value * 2;
                else
                    return Option<int>.None();
            });

//result.IsSome = false
//result.IsNone = true

Last updated