LanguageExt.Core

LanguageExt.Core Monads Alternative Monads Option

Option monads support either an A (success) value, or a None (no-value) option. You can think of this as an alternative to using null to represent a lack of a value. null unfortunately still allows you to . into the interface of the decalred type, which means there's a ticking time-bomb in every reference type.

C# does now have the nullable references feature, which goes some way to removing the need for an optional type, however there's still edge cases that mean the reference types are problematic. It's also useful to build generic types and say this is an Option<A> - I don't care if it's a value-type or reference-type, it's optional.

And finally, there's the automatic checking of None values when using Option<A> in LINQ expressions, or if you call Map. This makes working with optional values, and the implications for all of the code that works with it, fully declarative.

Here we have two flavours of Option:

  1. Option<A> the default optional monad. It does not allow null in its Some case.
  2. OptionUnsafe<A> as above, but it does allow null in its Some case.

You can construct a Some using the constructor functions in the Prelude:

Option<int> ma = Some(123);
Option<int> mb = None;

Contents

Sub modules

Option-Shared

struct Option <A> Source #

Discriminated union type. Can be in one of two states:

Some(a)
None

Trait instances available for this type:

BiFoldable  : MOption
Eq          : EqOpt
Foldable    : MOption
Functor     : FOption
MonadPlus   : MOption
Optional    : MOption
Ord         : OrdOpt

Parameters

type A

Bound value

Fields

field Option<A> None = default Source #

None

Properties

property object? Case Source #

Reference version of option for use in pattern-matching

Some = result is A
None = result is null

property bool IsSome Source #

Is the option in a Some state

property bool IsNone Source #

Is the option in a None state

property Option<A> Empty Source #

Monoid empty (aka None)

Constructors

constructor Option (IEnumerable<A> option) Source #

Ctor that facilitates serialisation

Parameters

param option

None or Some A.

Methods

method Option<A> Some (A value) Source #

Construct an Option of A in a Some state

Parameters

param value

Value to bind, must be non-null

returns

Option of A

method void GetObjectData (SerializationInfo info, StreamingContext context) Source #

method bool Equals (Option<A> other) Source #

Uses the EqDefault instance to do an equality check on the bound value. To use anything other than the default call oa.Equals<EqA>(ob) where EqA is an instance derived from Eq<A>

This uses the EqDefault instance for comparison of the bound A values. The EqDefault instance wraps up the .NET EqualityComparer.Default behaviour.

Parameters

param other

The Option type to compare this type with

returns

True if this and other are equal

method bool Equals <EqA> (Option<A> other) Source #

where EqA : Eq<A>

Uses the EqA instance to do an equality check on the bound value.

Parameters

param other

The Option type to compare this type with

returns

True if this and other are equal

method int CompareTo (Option<A> other) Source #

Uses the OrdDefault instance to do an ordering comparison on the bound value. To use anything other than the default call this.Compare<OrdA>(this, other), where OrdA is an instance derived from Ord<A>

Parameters

param other

The Option type to compare this type with

method int CompareTo <OrdA> (Option<A> other) Source #

where OrdA : Ord<A>

Uses the Ord instance provided to do an ordering comparison on the bound value.

Parameters

param other

The Option type to compare this type with

method bool Equals (object? obj) Source #

DO NOT USE - Use the Structural equality variant of this method Equals<EQ, A>(y)

method int GetHashCode () Source #

Calculate the hash-code from the bound value, unless the Option is in a None state, in which case the hash-code will be 0

Parameters

returns

Hash-code from the bound value, unless the Option is in a None state, in which case the hash-code will be 0

method int CompareTo (object? obj) Source #

method string ToString () Source #

Get a string representation of the Option

Parameters

returns

String representation of the Option

method Option<A> Do (Action<A> f) Source #

Impure iteration of the bound value in the structure

Parameters

returns

Returns the original unmodified structure

method Option<B> Select <B> (Func<A, B> f) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param f

Projection function

returns

Mapped functor

method Option<B> Map <B> (Func<A, B> f) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param f

Projection function

returns

Mapped functor

method K<F, Option<B>> Traverse <F, B> (Func<A, K<F, B>> f) Source #

where F : Applicative<F>

Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.

Parameters

type F

Applicative functor trait

type B

Bound value (output)

param f
param ta

Traversable structure

method K<M, Option<B>> TraverseM <M, B> (Func<A, K<M, B>> f) Source #

where M : Monad<M>

Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.

Parameters

type M

Monad trait

type B

Bound value (output)

param f
param ta

Traversable structure

method Option<B> Bind <B> (Func<A, Option<B>> f) Source #

Monad bind operation

method Option<B> Bind <B> (Func<A, K<Option, B>> f) Source #

Monad bind operation

method Option<B> BiBind <B> (Func<A, Option<B>> Some, Func<Option<B>> None) Source #

Bi-bind. Allows mapping of both monad states

method Option<C> SelectMany <B, C> ( Func<A, Option<B>> bind, Func<A, B, C> project) Source #

Monad bind operation

method Option<C> SelectMany <C> ( Func<A, Fail<Unit>> bind, Func<A, Unit, C> project) Source #

Monad bind operation

method R MatchUntyped <R> (Func<object?, R> Some, Func<R> None) Source #

Match operation with an untyped value for Some. This can be useful for serialisation and dealing with the IOptional interface

Parameters

type R

The return type

param Some

Operation to perform if the option is in a Some state

param None

Operation to perform if the option is in a None state

returns

The result of the match operation

method Type GetUnderlyingType () Source #

Get the Type of the bound value

Parameters

returns

Type of the bound value

method Arr<A> ToArray () Source #

Convert the Option to an enumerable of zero or one items

Parameters

returns

An enumerable of zero or one items

method Lst<A> ToList () Source #

Convert the Option to an immutable list of zero or one items

Parameters

returns

An immutable list of zero or one items

method Seq<A> ToSeq () Source #

Convert the Option to an enumerable sequence of zero or one items

Parameters

returns

An enumerable sequence of zero or one items

method Iterable<A> AsIterable () Source #

Convert the Option to an enumerable of zero or one items

Parameters

returns

An enumerable of zero or one items

method Eff<A> ToEff () Source #

Convert the structure to an Eff

Parameters

returns

An Eff representation of the structure

method OptionT<IO, A> ToIO () Source #

Convert to an Option transformer with embedded IO

Parameters

returns

method StreamT<M, A> ToStream <M> () Source #

where M : Monad<M>

Convert to a stream

method Eff<A> ToEff (Error Fail) Source #

Convert the structure to an Eff

Parameters

param Fail

Default value if the structure is in a None state

returns

An Eff representation of the structure

method Fin<A> ToFin () Source #

Convert the structure to a Fin

Parameters

returns

A Fin representation of the structure

method Fin<A> ToFin (Error Fail) Source #

Convert the structure to a Fin

Parameters

param Fail

Default value if the structure is in a None state

returns

A Fin representation of the structure

method Either<L, A> ToEither <L> (L defaultLeftValue) Source #

Convert the structure to an Either

Parameters

param defaultLeftValue

Default value if the structure is in a None state

returns

An Either representation of the structure

method Either<L, A> ToEither <L> () Source #

where L : Monoid<L>

Convert the structure to an Either

method Either<L, A> ToEither <L> (Func<L> Left) Source #

Convert the structure to an Either

Parameters

param Left

Function to invoke to get a default value if the structure is in a None state

returns

An Either representation of the structure

method Validation<L, A> ToValidation <L> (Func<L> Fail) Source #

where L : Monoid<L>

Convert the structure to a Validation

Parameters

param Fail

Function to invoke to get a default value if the structure is in a None state

returns

An Validation representation of the structure

method Validation<L, A> ToValidation <L> (L Fail) Source #

where L : Monoid<L>

Convert the structure to a Validation

Parameters

param Fail

Default value if the structure is in a None state

returns

An Validation representation of the structure

method Validation<L, A> ToValidation <L> () Source #

where L : Monoid<L>

Convert the structure to a Validation

Parameters

returns

An Validation representation of the structure

method SomeUnitContext<A> Some (Action<A> f) Source #

Fluent pattern matching. Provide a Some handler and then follow on fluently with .None(...) to complete the matching operation. This is for dispatching actions, use Some<A,B>(...) to return a value from the match operation.

Parameters

param f

The Some(x) match operation

method SomeContext<A, B> Some <B> (Func<A, B> f) Source #

Fluent pattern matching. Provide a Some handler and then follow on fluently with .None(...) to complete the matching operation. This is for returning a value from the match operation, to dispatch an action instead, use Some(...)

Parameters

type B

Match operation return value type

param f

The Some(x) match operation

returns

The result of the match operation

method B Match <B> (Func<A, B> Some, Func<B> None) Source #

Match the two states of the Option and return a non-null R.

Parameters

type B

Return type

param Some

Some match operation. Must not return null.

param None

None match operation. Must not return null.

returns

A non-null B

method B Match <B> (Func<A, B> Some, B None) Source #

Match the two states of the Option and return a non-null R.

Parameters

type B

Return type

param Some

Some match operation. Must not return null.

param None

None match operation. Must not return null.

returns

A non-null B

method Unit Match (Action<A> Some, Action None) Source #

Match the two states of the Option

Parameters

param Some

Some match operation

param None

None match operation

method Unit IfSome (Action<A> f) Source #

Invokes the action if Option is in the Some state, otherwise nothing happens.

Parameters

param f

Action to invoke if Option is in the Some state

method Unit IfSome (Func<A, Unit> f) Source #

Invokes the f function if Option is in the Some state, otherwise nothing happens.

Parameters

param f

Function to invoke if Option is in the Some state

method A IfNone (Func<A> None) Source #

Returns the result of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.

Will not accept a null return value from the None operation

Parameters

param None

Operation to invoke if the structure is in a None state

returns

Result of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.

method Unit IfNone (Action None) Source #

Invokes the action if Option is in the None state, otherwise nothing happens.

Parameters

param f

Action to invoke if Option is in the None state

method A IfNone (A noneValue) Source #

Returns the noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned.

Will not accept a null noneValue

Parameters

param noneValue

Value to return if in a None state

returns

noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned

method S Fold <S> (S state, Func<S, A, S> folder) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param folder

Folder function, applied if Option is in a Some state

returns

The aggregate state

method S FoldBack <S> (S state, Func<S, A, S> folder) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param folder

Folder function, applied if Option is in a Some state

returns

The aggregate state

method S BiFold <S> (S state, Func<S, A, S> Some, Func<S, Unit, S> None) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param Some

Folder function, applied if Option is in a Some state

param None

Folder function, applied if Option is in a None state

returns

The aggregate state

method S BiFold <S> (S state, Func<S, A, S> Some, Func<S, S> None) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param Some

Folder function, applied if Option is in a Some state

param None

Folder function, applied if Option is in a None state

returns

The aggregate state

method Option<B> BiMap <B> (Func<A, B> Some, Func<Unit, B> None) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param Some

Projection function

param None

Projection function

returns

Mapped functor

method Option<B> BiMap <B> (Func<A, B> Some, Func<B> None) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param Some

Projection function

param None

Projection function

returns

Mapped functor

method int Count () Source #

Return the number of bound values in this structure:

None = 0

Some = 1

Parameters

returns

method bool ForAll (Func<A, bool> pred) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned (because the predicate applies for-all values). If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

Parameters

param pred
returns

If the Option is in a None state then True is returned (because the predicate applies for-all values). If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

method bool BiForAll (Func<A, bool> Some, Func<Unit, bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool BiForAll (Func<A, bool> Some, Func<bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool Exists (Func<A, bool> pred) Source #

Apply a predicate to the bound value. If the Option is in a None state then False is returned. If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

Parameters

param pred
returns

If the Option is in a None state then False is returned. If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

method bool BiExists (Func<A, bool> Some, Func<Unit, bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param pred
returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool BiExists (Func<A, bool> Some, Func<bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param pred
returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method Unit Iter (Action<A> Some) Source #

Invoke an action for the bound value (if in a Some state)

Parameters

param Some

Action to invoke

method Unit BiIter (Action<A> Some, Action<Unit> None) Source #

Invoke an action depending on the state of the Option

Parameters

param Some

Action to invoke if in a Some state

param None

Action to invoke if in a None state

method Unit BiIter (Action<A> Some, Action None) Source #

Invoke an action depending on the state of the Option

Parameters

param Some

Action to invoke if in a Some state

param None

Action to invoke if in a None state

method Option<A> Filter (Func<A, bool> pred) Source #

Apply a predicate to the bound value (if in a Some state)

Parameters

param pred

Predicate to apply

returns

Some(x) if the Option is in a Some state and the predicate returns True. None otherwise.

method Option<A> Where (Func<A, bool> pred) Source #

Apply a predicate to the bound value (if in a Some state)

Parameters

param pred

Predicate to apply

returns

Some(x) if the Option is in a Some state and the predicate returns True. None otherwise.

method Option<Func<B, C>> ParMap <B, C> (Func<A, B, C> func) Source #

Partial application map

method Option<Func<B, Func<C, D>>> ParMap <B, C, D> (Func<A, B, C, D> func) Source #

Partial application map

method IEnumerator<A> GetEnumerator () Source #

Get an enumerator for the Option

method Option<B> Bind <B> (Func<A, Pure<B>> f) Source #

Monadic bind

Parameters

param f

Bind function

method Option<B> Bind <B> (Func<A, Fail<Unit>> f) Source #

Monadic bind

Parameters

param f

Bind function

method Option<C> SelectMany <B, C> (Func<A, Pure<B>> bind, Func<A, B, C> project) Source #

Monadic bind and project

Parameters

param bind

Bind function

param project

Project function

method Option<A> Combine (Option<A> rhs) Source #

Semigroup combine

Parameters

param rhs

Alternative to return if this is None

returns

This if in a Some state, rhs otherwise

Operators

operator < (Option<A> lhs, Option<A> rhs) Source #

Comparison operator

Parameters

param lhs

The left hand side of the operation

param rhs

The right hand side of the operation

returns

True if lhs < rhs

operator <= (Option<A> lhs, Option<A> rhs) Source #

Comparison operator

Parameters

param lhs

The left hand side of the operation

param rhs

The right hand side of the operation

returns

True if lhs <= rhs

operator > (Option<A> lhs, Option<A> rhs) Source #

Comparison operator

Parameters

param lhs

The left hand side of the operation

param rhs

The right hand side of the operation

returns

True if lhs > rhs

operator >= (Option<A> lhs, Option<A> rhs) Source #

Comparison operator

Parameters

param lhs

The left hand side of the operation

param rhs

The right hand side of the operation

returns

True if lhs >= rhs

operator == (Option<A> lhs, Option<A> rhs) Source #

Equality operator

This uses the EqDefault instance for comparison of the bound A values. The EqDefault instance wraps up the .NET EqualityComparer.Default behaviour. For more control over equality you can call:

equals<EQ, A>(lhs, rhs);

Where EQ is a struct derived from Eq. For example:

equals<EqString, string>(lhs, rhs);
equals<EqArray<int>, int[]>(lhs, rhs);

Parameters

param lhs

Left hand side of the operation

param rhs

Right hand side of the operation

returns

True if the values are equal

operator != (Option<A> lhs, Option<A> rhs) Source #

Non-equality operator

This uses the EqDefault instance for comparison of the A value. The EqDefault trait wraps up the .NET EqualityComparer.Default behaviour. For more control over equality you can call:

!equals<EQ, A>(lhs, rhs);

Where EQ is a struct derived from Eq. For example:

!equals<EqString, string>(lhs, rhs);
!equals<EqArray<int>, int[]>(lhs, rhs);

Parameters

param lhs

Left hand side of the operation

param rhs

Right hand side of the operation

returns

True if the values are equal

operator | (Option<A> lhs, Option<A> rhs) Source #

Coalescing operator

Parameters

param lhs

Left hand side of the operation

param rhs

Right hand side of the operation

returns

if lhs is Some then lhs, else rhs

operator | (K<Option, A> lhs, Option<A> rhs) Source #

operator | (Option<A> lhs, K<Option, A> rhs) Source #

operator | (Option<A> ma, Pure<A> mb) Source #

operator | (Option<A> ma, Fail<Unit> mb) Source #

operator | (Option<A> ma, CatchM<Unit, Option, A> mb) Source #

operator true (Option<A> value) Source #

Truth operator

operator false (Option<A> value) Source #

Falsity operator

class OptionExtensions Source #

Extension methods for Option

Methods

method Option<A> As <A> (this K<Option, A> ma) Source #

method Validation<F, A> ToValidation <F, A> (Option<A> ma, F defaultFailureValue) Source #

where F : Monoid<F>

method Option<A> Flatten <A> (this Option<Option<A>> ma) Source #

Monadic join

method IEnumerable<A> Somes <A> (this IEnumerable<Option<A>> self) Source #

Extracts from a list of Option all the Some elements. All the Some elements are extracted in order.

method Seq<A> Somes <A> (this Seq<Option<A>> self) Source #

Extracts from a list of Option all the Some elements. All the Some elements are extracted in order.

method Option<A> Add <ARITH, A> (this Option<A> x, Option<A> y) Source #

where ARITH : Arithmetic<A>

Add the bound values of x and y, uses an Add trait to provide the add operation for type A. For example x.Add<TInteger,int>(y)

Parameters

type ADD

Add of A

type A

Bound value type

param x

Left hand side of the operation

param y

Right hand side of the operation

returns

An option with y added to x

method Option<A> Subtract <ARITH, A> (this Option<A> x, Option<A> y) Source #

where ARITH : Arithmetic<A>

Find the subtract between the two bound values of x and y, uses a Subtract trait to provide the subtract operation for type A. For example x.Subtract<TInteger,int>(y)

Parameters

type DIFF

Subtract of A

type A

Bound value type

param x

Left hand side of the operation

param y

Right hand side of the operation

returns

An option with the subtract between x and y

method Option<A> Product <ARITH, A> (this Option<A> x, Option<A> y) Source #

where ARITH : Arithmetic<A>

Find the product between the two bound values of x and y, uses a Product trait to provide the product operation for type A. For example x.Product<TInteger,int>(y)

Parameters

type PROD

Product of A

type A

Bound value type

param x

Left hand side of the operation

param y

Right hand side of the operation

returns

An option with the product of x and y

method Option<A> Divide <NUM, A> (this Option<A> x, Option<A> y) Source #

where NUM : Num<A>

Divide the two bound values of x and y, uses a Divide trait to provide the divide operation for type A. For example x.Divide<TDouble,double>(y)

Parameters

type DIV

Divide of A

type A

Bound value type

param x

Left hand side of the operation

param y

Right hand side of the operation

returns

An option x / y

method A? ToNullable <A> (this Option<A> ma) Source #

where A : struct

Convert the Option type to a Nullable of A

Parameters

type A

Type of the bound value

param ma

Option to convert

returns

Nullable of A

method R Match <R> (this Option<bool> ma, Func<R> True, Func<R> False, Func<R> None) Source #

Match for an optional boolean

Parameters

type R
param ma

Optional boolean

param True

Match for Some(true)

param False

Match for Some(false)

param None

Match for None

returns

method IEnumerable<R> Match <T, R> (this IEnumerable<Option<T>> list, Func<T, IEnumerable<R>> Some, Func<IEnumerable<R>> None) Source #

Match over a list of options

Parameters

type T

Type of the bound values

type R

Result type

param list

List of options to match against

param Some

Operation to perform when an Option is in the Some state

param None

Operation to perform when an Option is in the None state

returns

An enumerable of results of the match operations

method IEnumerable<R> Match <T, R> (this IEnumerable<Option<T>> list, Func<T, IEnumerable<R>> Some, IEnumerable<R> None) Source #

Match over a list of options

Parameters

type T

Type of the bound values

type R

Result type

param list

List of options to match against

param Some

Operation to perform when an Option is in the Some state

param None

Default if the list is empty

returns

An enumerable of results of the match operations

class OptionExtensions Source #

Methods

method Option<B> Map <A, B> (this Func<A, B> f, K<Option, A> ma) Source #

Functor map operation

Unwraps the value within the functor, passes it to the map function f provided, and then takes the mapped value and wraps it back up into a new functor.

Parameters

param ma

Functor to map

param f

Mapping function

returns

Mapped functor

method Option<B> Map <A, B> (this Func<A, B> f, Option<A> ma) Source #

Functor map operation

Unwraps the value within the functor, passes it to the map function f provided, and then takes the mapped value and wraps it back up into a new functor.

Parameters

param ma

Functor to map

param f

Mapping function

returns

Mapped functor

method Option<B> Action <A, B> (this Option<A> ma, K<Option, B> mb) Source #

Applicative action: runs the first applicative, ignores the result, and returns the second applicative

method Option<B> Action <A, B> (this K<Option, A> ma, K<Option, B> mb) Source #

Applicative action: runs the first applicative, ignores the result, and returns the second applicative

method Option<B> Apply <A, B> (this Option<Func<A, B>> mf, K<Option, A> ma) Source #

Applicative functor apply operation

Unwraps the value within the ma applicative-functor, passes it to the unwrapped function(s) within mf, and then takes the resulting value and wraps it back up into a new applicative-functor.

Parameters

param ma

Value(s) applicative functor

param mf

Mapping function(s)

returns

Mapped applicative functor

method Option<B> Apply <A, B> (this K<Option, Func<A, B>> mf, K<Option, A> ma) Source #

Applicative functor apply operation

Unwraps the value within the ma applicative-functor, passes it to the unwrapped function(s) within mf, and then takes the resulting value and wraps it back up into a new applicative-functor.

Parameters

param ma

Value(s) applicative functor

param mf

Mapping function(s)

returns

Mapped applicative functor

class Option Source #

class Prelude Source #

Properties

property Fail<Unit> None Source #

'No value' state of Option T.

Methods

method Option<A> flatten <A> (Option<Option<A>> ma) Source #

Monadic join

method Option<T> subtract <NUM, T> (Option<T> lhs, Option<T> rhs) Source #

where NUM : Num<T>

Subtract the Ts

Parameters

param lhs

Left-hand side of the operation

param rhs

Right-hand side of the operation

returns

lhs - rhs

method Option<T> product <NUM, T> (Option<T> lhs, Option<T> rhs) Source #

where NUM : Num<T>

method Option<T> divide <NUM, T> (Option<T> lhs, Option<T> rhs) Source #

where NUM : Num<T>

Divide the Ts

Parameters

param lhs

Left-hand side of the operation

param rhs

Right-hand side of the operation

returns

lhs / rhs

method Option<T> add <NUM, T> (Option<T> lhs, Option<T> rhs) Source #

where NUM : Num<T>

Add the Ts

Parameters

param lhs

Left-hand side of the operation

param rhs

Right-hand side of the operation

returns

lhs / rhs

method bool isSome <T> (Option<T> value) Source #

Check if Option is in a Some state

Parameters

type T

T

param value

Option

returns

True if value is in a Some state

method bool isNone <T> (Option<T> value) Source #

Check if Option is in a None state

Parameters

type T

T

param value

Option

returns

True if value is in a None state

method Option<A> Some <A> (A value) Source #

Create a Some of A

Parameters

type A

Bound value type

param value

Non-null value to be made optional

returns

Option<A> in a Some state or throws ValueIsNullException if isnull(value).

method Option<A> Some <A> (A? value) Source #

where A : struct

Create a Some of A from a Nullable<A>

Parameters

type A

Bound value type

param value

Non-null value to be made optional

returns

Option<A> in a Some state or throws ValueIsNullException if isnull(value)

method Option<A> Optional <A> (A? value) Source #

Create an Option of A

Parameters

type A

Bound value type

param value

Value to be made optional, or null

returns

If the value is null it will be None else Some(value)

method Option<A> Optional <A> (A? value) Source #

where A : struct

Create an Option of A

Parameters

type A

Bound value type

param value

Value to be made optional, or null

returns

If the value is null it will be None else Some(value)

method Unit ifSome <T> (Option<T> option, Action<T> Some) Source #

Invokes the action if Option is in the Some state, otherwise nothing happens.

Parameters

param f

Action to invoke if Option is in the Some state

method T ifNone <T> (Option<T> option, Func<T> None) Source #

Returns the result of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.

Will not accept a null return value from the None operation

Parameters

param None

Operation to invoke if the structure is in a None state

returns

Tesult of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.

method T ifNone <T> (Option<T> option, T noneValue) Source #

Returns the noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned.

Will not accept a null noneValue

Parameters

param noneValue

Value to return if in a None state

returns

noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned

method R match <T, R> (Option<T> option, Func<T, R> Some, Func<R> None) Source #

Match the two states of the Option and return a non-null R.

Parameters

type B

Return type

param Some

Some match operation. Must not return null.

param None

None match operation. Must not return null.

returns

A non-null B

method Unit match <T> (Option<T> option, Action<T> Some, Action None) Source #

Match the two states of the Option

Parameters

param Some

Some match operation

param None

None match operation

method S bifold <S, A> (Option<A> option, S state, Func<S, A, S> Some, Func<S, S> None) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param Some

Folder function, applied if Option is in a Some state

param None

Folder function, applied if Option is in a None state

returns

The aggregate state

method S bifold <S, A> (Option<A> option, S state, Func<S, A, S> Some, Func<S, Unit, S> None) Source #

Option types are like lists of 0 or 1 items, and therefore follow the same rules when folding.

In the case of lists, 'Fold', when applied to a binary operator, a starting value(typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:

Note that, since the head of the resulting expression is produced by an application of the operator to the first element of the list, 'Fold' can produce a terminating expression from an infinite list.

Parameters

type S

Aggregate state type

param state

Initial state

param Some

Folder function, applied if Option is in a Some state

param None

Folder function, applied if Option is in a None state

returns

The aggregate state

method bool forall <A> (Option<A> option, Func<A, bool> pred) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned (because the predicate applies for-all values). If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

Parameters

param pred

Predicate to apply

returns

If the Option is in a None state then True is returned (because the predicate applies for-all values). If the Option is in a Some state the value is the result of running applying the bound value to the predicate supplied.

method bool biforall <A> (Option<A> option, Func<A, bool> Some, Func<Unit, bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool biforall <A> (Option<A> option, Func<A, bool> Some, Func<bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method int count <A> (Option<A> option) Source #

Return the number of bound values in this structure:

None = 0

Some = 1

Parameters

returns

method bool exists <A> (Option<A> option, Func<A, bool> pred) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param pred

Predicate to apply

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool biexists <A> (Option<A> option, Func<A, bool> Some, Func<Unit, bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method bool biexists <A> (Option<A> option, Func<A, bool> Some, Func<bool> None) Source #

Apply a predicate to the bound value. If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

Parameters

param Some

Predicate to apply if in a Some state

param None

Predicate to apply if in a None state

returns

If the Option is in a None state then True is returned if invoking None returns True. If the Option is in a Some state the value is the result of running applying the bound value to the Some predicate supplied.

method Option<B> map <A, B> (Option<A> option, Func<A, B> f) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param f

Projection function

returns

Mapped functor

method Option<B> bimap <A, B> (Option<A> option, Func<A, B> Some, Func<B> None) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param Some

Projection function

param None

Projection function

returns

Mapped functor

method Option<B> bimap <A, B> (Option<A> option, Func<A, B> Some, Func<Unit, B> None) Source #

Projection from one value to another

Parameters

type B

Resulting functor value type

param Some

Projection function

param None

Projection function

returns

Mapped functor

method Option<T> filter <T> (Option<T> option, Func<T, bool> pred) Source #

Apply a predicate to the bound value (if in a Some state)

Parameters

param pred

Predicate to apply

returns

Some(x) if the Option is in a Some state and the predicate returns True. None otherwise.

method Option<R> bind <T, R> (Option<T> option, Func<T, Option<R>> binder) Source #

Monadic bind operation

method IEnumerable<R> match <T, R> ( IEnumerable<Option<T>> list, Func<T, IEnumerable<R>> Some, Func<IEnumerable<R>> None) Source #

Match the two states of the list of Options

Parameters

param Some

Some match operation

param None

None match operation

method IEnumerable<R> match <T, R> (IEnumerable<Option<T>> list, Func<T, IEnumerable<R>> Some, IEnumerable<R> None) Source #

Match the two states of the list of Options

Parameters

param Some

Some match operation

param None

None match operation

method IEnumerable<T> somes <T> (IEnumerable<Option<T>> list) Source #

Extracts from a list of 'Option' all the 'Some' elements. All the 'Some' elements are extracted in order.

method Lst<T> toList <T> (Option<T> option) Source #

Convert the Option to an immutable list of zero or one items

Parameters

returns

An immutable list of zero or one items

method Arr<T> toArray <T> (Option<T> option) Source #

Convert the Option to an enumerable of zero or one items

Parameters

returns

An enumerable of zero or one items

class Prelude Source #

Methods

method Option<B> map <A, B> (Func<A, B> f, K<Option, A> ma) Source #

Functor map operation

Unwraps the value within the functor, passes it to the map function f provided, and then takes the mapped value and wraps it back up into a new functor.

Parameters

param ma

Functor to map

param f

Mapping function

returns

Mapped functor

method Option<B> action <A, B> (K<Option, A> ma, K<Option, B> mb) Source #

Applicative action: runs the first applicative, ignores the result, and returns the second applicative

method Option<B> apply <A, B> (K<Option, Func<A, B>> mf, K<Option, A> ma) Source #

Applicative functor apply operation

Unwraps the value within the ma applicative-functor, passes it to the unwrapped function(s) within mf, and then takes the resulting value and wraps it back up into a new applicative-functor.

Parameters

param ma

Value(s) applicative functor

param mf

Mapping function(s)

returns

Mapped applicative functor