Option<A> Struct

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

Some(a)
None


Typeclass instances available for this type:

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

DECLARATION
[Serializable]
public struct Option<A> : IEnumerable<A>,
IOptional,
IEquatable<Option<A>>,
IComparable<Option<A>>,
ISerializable
NAMESPACE
LanguageExt

Constructors

Option(SerializationInfo, StreamingContext)
PARAMETERS
info
SerializationInfo
context
StreamingContext
DECLARATION
[Pure]
public ctor Option(SerializationInfo info, StreamingContext context)

Fields

None
SUMMARY

None

DECLARATION
public static readonly Option<A> None

Properties

IsLazy
SUMMARY

True if this instance evaluates lazily

DECLARATION
[Pure]
public bool IsLazy { get; }
RETURNS
bool
IsNone
SUMMARY

Is the option in a None state

DECLARATION
[Pure]
public bool IsNone { get; }
RETURNS
bool
IsSome
SUMMARY

Is the option in a Some state

DECLARATION
[Pure]
public bool IsSome { get; }
RETURNS
bool

Methods

AsEnumerable()
SUMMARY

Convert the Option to an enumerable of zero or one items

DECLARATION
[Pure]
public Seq<A> AsEnumerable()
RETURNS
Seq<A>
An enumerable of zero or one items
BiBind<B>(Func<A, Option<B>>, Func<Option<B>>)
SUMMARY

Bi-bind. Allows mapping of both monad states

PARAMETERS
Some
Func<A, Option<B>>
None
Func<Option<B>>
DECLARATION
[Pure]
public Option<B> BiBind<B>(Func<A, Option<B>> Some, Func<Option<B>> None)
RETURNS
Option<B>
BiExists(Func<A, bool>, Func<Unit, bool>)
SUMMARY

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
Some
Func<A, bool>
None
Func<Unit, bool>
DECLARATION
[Pure]
public bool BiExists(Func<A, bool> Some, Func<Unit, bool> None)
RETURNS
bool
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.
BiExists(Func<A, bool>, Func<bool>)
SUMMARY

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
Some
Func<A, bool>
None
Func<bool>
DECLARATION
[Pure]
public bool BiExists(Func<A, bool> Some, Func<bool> None)
RETURNS
bool
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.
BiFold<S>(S, Func<S, A, S>, Func<S, Unit, S>)
SUMMARY

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
state
S
Initial state
Some
Func<S, A, S>
Folder function, applied if Option is in a Some state
None
Func<S, Unit, S>
Folder function, applied if Option is in a None state
DECLARATION
[Pure]
public S BiFold<S>(S state, Func<S, A, S> Some, Func<S, Unit, S> None)
RETURNS
S
The aggregate state
BiFold<S>(S, Func<S, A, S>, Func<S, S>)
SUMMARY

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
state
S
Initial state
Some
Func<S, A, S>
Folder function, applied if Option is in a Some state
None
Func<S, S>
Folder function, applied if Option is in a None state
DECLARATION
[Pure]
public S BiFold<S>(S state, Func<S, A, S> Some, Func<S, S> None)
RETURNS
S
The aggregate state
BiForAll(Func<A, bool>, Func<Unit, bool>)
SUMMARY

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
Some
Func<A, bool>
Predicate to apply if in a Some state
None
Func<Unit, bool>
Predicate to apply if in a None state
DECLARATION
[Pure]
public bool BiForAll(Func<A, bool> Some, Func<Unit, bool> None)
RETURNS
bool
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.
BiForAll(Func<A, bool>, Func<bool>)
SUMMARY

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
Some
Func<A, bool>
Predicate to apply if in a Some state
None
Func<bool>
Predicate to apply if in a None state
DECLARATION
[Pure]
public bool BiForAll(Func<A, bool> Some, Func<bool> None)
RETURNS
bool
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.
BiIter(Action<A>, Action<Unit>)
SUMMARY

Invoke an action depending on the state of the Option

PARAMETERS
Some
Action<A>
Action to invoke if in a Some state
None
Action<Unit>
Action to invoke if in a None state
DECLARATION
[Pure]
public Unit BiIter(Action<A> Some, Action<Unit> None)
BiIter(Action<A>, Action)
SUMMARY

Invoke an action depending on the state of the Option

PARAMETERS
Some
Action<A>
Action to invoke if in a Some state
None
Action
Action to invoke if in a None state
DECLARATION
[Pure]
public Unit BiIter(Action<A> Some, Action None)
BiMap<B>(Func<A, B>, Func<Unit, B>)
SUMMARY

Projection from one value to another

PARAMETERS
Some
Func<A, B>
Projection function
None
Func<Unit, B>
Projection function
DECLARATION
[Pure]
public Option<B> BiMap<B>(Func<A, B> Some, Func<Unit, B> None)
RETURNS
Option<B>
Mapped functor
BiMap<B>(Func<A, B>, Func<B>)
SUMMARY

Projection from one value to another

PARAMETERS
Some
Func<A, B>
Projection function
None
Func<B>
Projection function
DECLARATION
[Pure]
public Option<B> BiMap<B>(Func<A, B> Some, Func<B> None)
RETURNS
Option<B>
Mapped functor
Bind<B>(Func<A, Option<B>>)
SUMMARY

Monad bind operation

PARAMETERS
f
Func<A, Option<B>>
DECLARATION
[Pure]
public Option<B> Bind<B>(Func<A, Option<B>> f)
RETURNS
Option<B>
CompareTo(Option<A>)
SUMMARY

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

PARAMETERS
other
Option<A>
The Option type to compare this type with
DECLARATION
public int CompareTo(Option<A> other)
RETURNS
int
True if this and other are equal
Count()
SUMMARY

Return the number of bound values in this structure:

None = 0


Some = 1


DECLARATION
[Pure]
public int Count()
RETURNS
int
Equals(Option<A>)
SUMMARY

Uses the EqDefault instance to do an equality check on the bound value. To use anything other than the default call equals<EQ, A>(a, b), where EQ is an instance derived from Eq<A>

PARAMETERS
other
Option<A>
The Option type to compare this type with
REMARKS
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<A>. For example:

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


DECLARATION
public bool Equals(Option<A> other)
RETURNS
bool
True if this and other are equal
Equals(object)
SUMMARY

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

PARAMETERS
obj
object
DECLARATION
[Pure]
public override bool Equals(object obj)
RETURNS
bool
Exists(Func<A, bool>)
SUMMARY

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
pred
Func<A, bool>
DECLARATION
[Pure]
public bool Exists(Func<A, bool> pred)
RETURNS
bool
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.
Filter(Func<A, bool>)
SUMMARY

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

PARAMETERS
pred
Func<A, bool>
Predicate to apply
DECLARATION
[Pure]
public Option<A> Filter(Func<A, bool> pred)
RETURNS
Option<A>
Some(x) if the Option is in a Some state and the predicate returns True. None otherwise.
Fold<S>(S, Func<S, A, S>)
SUMMARY

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
state
S
Initial state
folder
Func<S, A, S>
Folder function, applied if Option is in a Some state
DECLARATION
[Pure]
public S Fold<S>(S state, Func<S, A, S> folder)
RETURNS
S
The aggregate state
FoldBack<S>(S, Func<S, A, S>)
SUMMARY

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
state
S
Initial state
folder
Func<S, A, S>
Folder function, applied if Option is in a Some state
DECLARATION
[Pure]
public S FoldBack<S>(S state, Func<S, A, S> folder)
RETURNS
S
The aggregate state
ForAll(Func<A, bool>)
SUMMARY

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
pred
Func<A, bool>
DECLARATION
[Pure]
public bool ForAll(Func<A, bool> pred)
RETURNS
bool
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.
GetEnumerator()
SUMMARY

Get an enumerator for the Option

DECLARATION
public IEnumerator<A> GetEnumerator()
RETURNS
IEnumerator<A>
GetHashCode()
SUMMARY

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

DECLARATION
[Pure]
public override int GetHashCode()
RETURNS
int
Hash-code from the bound value, unless the Option is in a None state, in which case the hash-code will be 0
GetObjectData(SerializationInfo, StreamingContext)
PARAMETERS
info
SerializationInfo
context
StreamingContext
DECLARATION
public void GetObjectData(SerializationInfo info, StreamingContext context)
GetUnderlyingType()
SUMMARY

Get the Type of the bound value

DECLARATION
[Pure]
public Type GetUnderlyingType()
RETURNS
Type
Type of the bound value
IfNone(Func<A>)
SUMMARY

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

PARAMETERS
None
Func<A>
Operation to invoke if the structure is in a None state
REMARKS
Will not accept a null return value from the None operation
DECLARATION
[Pure]
public A IfNone(Func<A> None)
RETURNS
A
Tesult of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.
IfNone(A)
SUMMARY

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

PARAMETERS
noneValue
A
Value to return if in a None state
REMARKS
Will not accept a null noneValue
DECLARATION
[Pure]
public A IfNone(A noneValue)
RETURNS
A
noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned
IfNoneUnsafe(Func<A>)
SUMMARY

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

PARAMETERS
None
Func<A>
Operation to invoke if the structure is in a None state
REMARKS
Will allow null the be returned from the None operation
DECLARATION
[Pure]
public A IfNoneUnsafe(Func<A> None)
RETURNS
A
Tesult of invoking the None() operation if the optional is in a None state, otherwise the bound Some(x) value is returned.
IfNoneUnsafe(A)
SUMMARY

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

PARAMETERS
noneValue
A
Value to return if in a None state
REMARKS
Will allow noneValue to be null
DECLARATION
[Pure]
public A IfNoneUnsafe(A noneValue)
RETURNS
A
noneValue if the optional is in a None state, otherwise the bound Some(x) value is returned
IfSome(Action<A>)
SUMMARY

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

PARAMETERS
f
Action<A>
Action to invoke if Option is in the Some state
DECLARATION
public Unit IfSome(Action<A> f)
IfSome(Func<A, Unit>)
SUMMARY

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

PARAMETERS
f
Func<A, Unit>
Function to invoke if Option is in the Some state
DECLARATION
public Unit IfSome(Func<A, Unit> f)
Iter(Action<A>)
SUMMARY

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

PARAMETERS
Some
Action<A>
Action to invoke
DECLARATION
[Pure]
public Unit Iter(Action<A> Some)
Join<B, C, D>(Option<B>, Func<A, C>, Func<B, C>, Func<A, B, D>)
SUMMARY

Monadic join

PARAMETERS
inner
Option<B>
outerKeyMap
Func<A, C>
innerKeyMap
Func<B, C>
project
Func<A, B, D>
DECLARATION
[Pure]
public Option<D> Join<B, C, D>(Option<B> inner, Func<A, C> outerKeyMap, Func<B, C> innerKeyMap, Func<A, B, D> project)
RETURNS
Option<D>
Map<B>(Func<A, B>)
SUMMARY

Projection from one value to another

PARAMETERS
f
Func<A, B>
Projection function
DECLARATION
[Pure]
public Option<B> Map<B>(Func<A, B> f)
RETURNS
Option<B>
Mapped functor
Match(Action<A>, Action)
SUMMARY

Match the two states of the Option

PARAMETERS
Some
Action<A>
Some match operation
None
Action
None match operation
DECLARATION
public Unit Match(Action<A> Some, Action None)
Match<B>(Func<A, B>, Func<B>)
SUMMARY

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

PARAMETERS
Some
Func<A, B>
Some match operation. Must not return null.
None
Func<B>
None match operation. Must not return null.
DECLARATION
[Pure]
public B Match<B>(Func<A, B> Some, Func<B> None)
RETURNS
B
A non-null B
MatchUnsafe<B>(Func<A, B>, Func<B>)
SUMMARY

Match the two states of the Option and return a B, which can be null.

PARAMETERS
Some
Func<A, B>
Some match operation. May return null.
None
Func<B>
None match operation. May return null.
DECLARATION
[Pure]
public B MatchUnsafe<B>(Func<A, B> Some, Func<B> None)
RETURNS
B
B, or null
MatchUntyped<R>(Func<object, R>, Func<R>)
SUMMARY

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

PARAMETERS
Some
Func<object, R>
Operation to perform if the option is in a Some state
None
Func<R>
Operation to perform if the option is in a None state
DECLARATION
[Pure]
public R MatchUntyped<R>(Func<object, R> Some, Func<R> None)
RETURNS
R
The result of the match operation
ParMap<B, C, D>(Func<A, B, C, D>)
SUMMARY

Partial application map

PARAMETERS
func
Func<A, B, C, D>
REMARKS
TODO: Better documentation of this function
DECLARATION
[Pure]
public Option<Func<B, Func<C, D>>> ParMap<B, C, D>(Func<A, B, C, D> func)
RETURNS
Option<Func<B, Func<C, D>>>
ParMap<B, C>(Func<A, B, C>)
SUMMARY

Partial application map

PARAMETERS
func
Func<A, B, C>
REMARKS
TODO: Better documentation of this function
DECLARATION
[Pure]
public Option<Func<B, C>> ParMap<B, C>(Func<A, B, C> func)
RETURNS
Option<Func<B, C>>
Select<B>(Func<A, B>)
SUMMARY

Projection from one value to another

PARAMETERS
f
Func<A, B>
Projection function
DECLARATION
[Pure]
public Option<B> Select<B>(Func<A, B> f)
RETURNS
Option<B>
Mapped functor
SelectMany<B, C>(Func<A, Option<B>>, Func<A, B, C>)
SUMMARY

Monad bind operation

PARAMETERS
bind
Func<A, Option<B>>
project
Func<A, B, C>
DECLARATION
[Pure]
public Option<C> SelectMany<B, C>(Func<A, Option<B>> bind, Func<A, B, C> project)
RETURNS
Option<C>
Some(A)
SUMMARY

Construct an Option of A in a Some state

PARAMETERS
value
A
Value to bind, must be non-null
DECLARATION
[Pure]
public static Option<A> Some(A value)
RETURNS
Option<A>
Option of A
Some(Action<A>)
SUMMARY

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
f
Action<A>
The Some(x) match operation
DECLARATION
[Pure]
public SomeUnitContext<MOption<A>, Option<A>, A> Some(Action<A> f)
RETURNS
SomeUnitContext<MOption<A>, Option<A>, A>
Some<B>(Func<A, B>)
SUMMARY

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<A>(...)

PARAMETERS
f
Func<A, B>
The Some(x) match operation
DECLARATION
[Pure]
public SomeContext<MOption<A>, Option<A>, A, B> Some<B>(Func<A, B> f)
RETURNS
SomeContext<MOption<A>, Option<A>, A, B>
The result of the match operation
ToArray()
SUMMARY

Convert the Option to an enumerable of zero or one items

DECLARATION
[Pure]
public Arr<A> ToArray()
RETURNS
Arr<A>
An enumerable of zero or one items
ToEither<L>(L)
SUMMARY

Convert the structure to an Either

PARAMETERS
defaultLeftValue
L
Default value if the structure is in a None state
DECLARATION
[Pure]
public Either<L, A> ToEither<L>(L defaultLeftValue)
RETURNS
Either<L, A>
An Either representation of the structure
ToEither<L>(Func<L>)
SUMMARY

Convert the structure to an Either

PARAMETERS
Left
Func<L>
DECLARATION
[Pure]
public Either<L, A> ToEither<L>(Func<L> Left)
RETURNS
Either<L, A>
An Either representation of the structure
ToEitherUnsafe<L>(L)
SUMMARY

Convert the structure to an EitherUnsafe

PARAMETERS
defaultLeftValue
L
Default value if the structure is in a None state
DECLARATION
[Pure]
public EitherUnsafe<L, A> ToEitherUnsafe<L>(L defaultLeftValue)
RETURNS
EitherUnsafe<L, A>
An EitherUnsafe representation of the structure
ToEitherUnsafe<L>(Func<L>)
SUMMARY

Convert the structure to an EitherUnsafe

PARAMETERS
Left
Func<L>
DECLARATION
[Pure]
public EitherUnsafe<L, A> ToEitherUnsafe<L>(Func<L> Left)
RETURNS
EitherUnsafe<L, A>
An EitherUnsafe representation of the structure
ToList()
SUMMARY

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

DECLARATION
[Pure]
public Lst<A> ToList()
RETURNS
Lst<A>
An immutable list of zero or one items
ToOptionUnsafe()
SUMMARY

Convert the structure to a OptionUnsafe

DECLARATION
[Pure]
public OptionUnsafe<A> ToOptionUnsafe()
RETURNS
OptionUnsafe<A>
An OptionUnsafe representation of the structure
ToSeq()
SUMMARY

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

DECLARATION
[Pure]
public Seq<A> ToSeq()
RETURNS
Seq<A>
An enumerable sequence of zero or one items
ToString()
SUMMARY

Get a string representation of the Option

DECLARATION
[Pure]
public override string ToString()
RETURNS
string
String representation of the Option
ToTryOption()
SUMMARY

Convert the structure to a TryOption

DECLARATION
[Pure]
public TryOption<A> ToTryOption()
RETURNS
TryOption<A>
A TryOption representation of the structure
ToValidation<FAIL>(FAIL)
PARAMETERS
defaultFailureValue
FAIL
DECLARATION
[Pure]
public Validation<FAIL, A> ToValidation<FAIL>(FAIL defaultFailureValue)
RETURNS
Validation<FAIL, A>
Where(Func<A, bool>)
SUMMARY

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

PARAMETERS
pred
Func<A, bool>
Predicate to apply
DECLARATION
[Pure]
public Option<A> Where(Func<A, bool> pred)
RETURNS
Option<A>
Some(x) if the Option is in a Some state and the predicate returns True. None otherwise.