Maybe
Namespace: System · class
Provides a set of static methods to work with the Maybe monad, enabling functional programming patterns in C#.
Methods
Apply
static Maybe<TResult> Apply<T, TResult>(T value, Func<T, TResult> lambda)
Immediately apply a function to a value, and capture the result into a Maybe
static Maybe<TResult> Apply<T, TResult>(T value, Func<T, Maybe<TResult>> lambda)
Immediately apply a function to a value, and capture the result into a Maybe
static Maybe<TResult> Apply<T, TResult>(Maybe<T> value, Func<T, TResult> lambda)
Immediately apply a function to a value, and capture the result into a Maybe
static Maybe<TResult> Apply<T, TResult>(Maybe<T> value, Func<T, Maybe<TResult>> lambda)
Immediately apply a function to a value, and capture the result into a Maybe
Bind
static Func<Maybe<T>, Maybe<TResult>> Bind<T, TIntermediate, TResult>(Func<T, Maybe<TIntermediate>> f, Func<TIntermediate, Maybe<TResult>> g)
static Func<Maybe<TU>, Maybe<TResult>> Bind<TU, TIntermediate, TResult>(Func<TU, Maybe<TIntermediate>> f, Func<Maybe<TIntermediate>, Maybe<TResult>> g)
Error
static Maybe<T> Error<T>(Exception error)
Creates a Maybe instance representing an error.
error— The exception to encapsulate.
Returns: A Maybe instance encapsulating the error.
static Maybe<T> Error<T>(ExceptionDispatchInfo error)
Creates a Maybe instance representing an error.
error— The exception to encapsulate.
Returns: A Maybe instance encapsulating the error.
static Maybe<T> Error<T>(T _, Exception error)
Creates a Maybe instance representing an error, using the compiler to infer the type of the value.
_— A parameter whose value is ignored, used only to help the compiler infer the type.error— The exception to encapsulate.
Returns: A Maybe encapsulating the error.
static Maybe<T> Error<T>(T _, ExceptionDispatchInfo error)
Creates a Maybe instance representing an exception, using the compiler to infer the type of the value.
_— A parameter whose value is ignored, used only to help the compiler infer the type.error— The exception to encapsulate.
Returns: A Maybe instance encapsulating the error.
static Maybe<T> Error<T>(T _, Exception error0, Exception error1)
Creates a Maybe instance that encapsulates one or two exceptions.
_— A parameter whose value is ignored, used to help the compiler infer the type.error0— The first exception, which can be null.error1— The second exception, which can be null.
Returns: A Maybe instance encapsulating the error(s). If both errors are present, they are combined into an AggregateException.
FromTask
static Maybe<T> FromTask<T>(Task<T> task)
Convert a completed Task into an equivalent Maybe
static Maybe<T> FromTask<T>(Task<Maybe<T>> task)
Convert a completed Task with T being a Maybe, into an equivalent Maybe
Nothing
static Maybe<T> Nothing<T>()
Creates an empty Maybe instance.
Returns: An empty Maybe instance.
static Maybe<T> Nothing<T>(T _)
Creates an empty Maybe instance.
_— A parameter whose value is ignored, used only to help the compiler infer the type.
Returns: An empty Maybe instance.
OrDefault
static T OrDefault<T>(Maybe<T> m, T default = null)
Returns the value of the Maybe if it has one, or the specified default value.
Returns: The value of the Maybe if it has one; otherwise, the specified default value.
Return
static Maybe<T> Return<T>(T value)
Returns a Maybe instance containing the specified T.
value— The value to wrap in a Maybe.
Returns: A Maybe encapsulating the value.
If value is null, the Maybe still has a value, and is not equal to Nothing.
If you need to map null values to Nothing, use ReturnNotNull instead.
ReturnNotNull
static Maybe<T> ReturnNotNull<T>(T value)
Returns a Maybe instance containing the specified T, if it is not null.
value— The value to wrap in a Maybe, which could be null.
Returns: Nothing if the instance is null, otherwise a Maybe encapsulating the value.
static Maybe<T> ReturnNotNull<T>(T? value)
Returns a Maybe instance containing the specified Nullable.
value— The value to wrap in a Maybe.
Returns: Nothing if the instance is null, otherwise a Maybe encapsulating the value.
ToNullable
static T? ToNullable<T>(Maybe<T> m)
Converts a Maybe into a Nullable.
m— The instance to convert.
Returns: Returns either null if the instance is Nothing, or a T if the value is present.
Unwrap
static Task<Maybe<T>> Unwrap<T>(Task<Maybe<T>> task)
Streamline a potentially failed Task<Maybe