Method SomeIf
- Namespace
- SodaFlow.Functional
- Assembly
- SodaFlow.Functional.dll
SomeIf<T>(bool, T)
Creates a Maybe<T> containing the given value if a condition holds, and one containing nothing if it does not.
[Pure]
public static Maybe<T> SomeIf<T>(bool condition, T value)
Parameters
conditionboolTrue to hold the value.
valueTThe value to contain when
conditionholds.
Returns
Type Parameters
TThe type of the value, inferred from
value.
Remarks
This is the translation of an if that gives a value in one branch
and has nothing to give in the other. This code evaluates
value in each condition, because it is an argument.
Where that is not necessary, use
SomeIf<T>(bool, Func<T>).
SomeIf<T>(bool, Func<T>)
Creates a Maybe<T> containing the result of the given function if a condition holds, and one containing nothing if it does not.
[Pure]
public static Maybe<T> SomeIf<T>(bool condition, Func<T> valueFactory)
Parameters
conditionboolTrue to make a value and hold it.
valueFactoryFunc<T>Run to give the value when
conditionholds.
Returns
- Maybe<T>
A Maybe<T> containing the result of
valueFactoryifconditionis true, and one containing no value otherwise.
Type Parameters
TThe type of the value, inferred from
valueFactory.
Remarks
valueFactory runs only when
condition holds. Thus, use this version when the value
has a high cost, or when the value is only correct in that condition.