Either<L, R> Struct

Either L R Holds one of two values 'Left' or 'Right'. Usually 'Left' is considered 'wrong' or 'in error', and 'Right' is, well, right. So when the Either is in a Left state, it cancels computations like bind or map, etc. So you can see Left as an 'early out, with a message'. Unlike Option that has None as its alternative value (i.e. it has an 'early out, but no message').

DECLARATION
[Serializable]
public struct Either<L, R> : IEnumerable<EitherData<L, R>>,
IEither,
IComparable<Either<L, R>>,
IComparable<R>,
IEquatable<Either<L, R>>,
IEquatable<R>,
ISerializable
NAMESPACE
LanguageExt
REMARKS
NOTE: If you use Filter or Where (or 'where' in a LINQ expression) with Either, then the Either will be put into a 'Bottom' state if the predicate returns false. When it's in this state it is neither Right nor Left. And any usage could trigger a BottomException. So be aware of the issue of filtering Either.

Also note, when the Either is in a Bottom state, some operations on it will continue to give valid results or return another Either in the Bottom state and not throw. This is so a filtered Either doesn't needlessly break expressions.

Constructors

Either(SerializationInfo, StreamingContext)
PARAMETERS
info
SerializationInfo
context
StreamingContext
DECLARATION
[Pure]
public ctor Either(SerializationInfo info, StreamingContext context)
Either(IEnumerable<EitherData<L, R>>)
SUMMARY

Ctor that facilitates serialisation

PARAMETERS
either
IEnumerable<EitherData<L, R>>
DECLARATION
[Pure]
public ctor Either(IEnumerable<EitherData<L, R>> either)

Fields

Bottom
DECLARATION
public static readonly Either<L, R> Bottom
State
SUMMARY

State of the Either You can also use:

IsRight
IsLeft
IsBottom

DECLARATION
public readonly EitherStatus State

Properties

IsBottom
SUMMARY

Is the Either in a Bottom state? When the Either is filtered, both Right and Left are meaningless.

If you use Filter or Where (or 'where' in a LINQ expression) with Either, then the Either will be put into a 'Bottom' state if the predicate returns false. When it's in this state it is neither Right nor Left. And any usage could trigger a BottomException. So be aware of the issue of filtering Either.

Also note, when the Either is in a Bottom state, some operations on it will continue to give valid results or return another Either in the Bottom state and not throw. This is so a filtered Either doesn't needlessly break expressions.

DECLARATION
[Pure]
public bool IsBottom { get; }
RETURNS
bool
IsLeft
SUMMARY

Is the Either in a Left state?

DECLARATION
[Pure]
public bool IsLeft { get; }
RETURNS
bool
IsRight
SUMMARY

Is the Either in a Right state?

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

Methods

BiBind<B>(Func<R, Either<L, B>>, Func<L, Either<L, B>>)
SUMMARY

Bi-bind. Allows mapping of both monad states

PARAMETERS
Right
Func<R, Either<L, B>>
Left
Func<L, Either<L, B>>
DECLARATION
[Pure]
public Either<L, B> BiBind<B>(Func<R, Either<L, B>> Right, Func<L, Either<L, B>> Left)
RETURNS
Either<L, B>
BiExists(Func<R, bool>, Func<L, bool>)
SUMMARY

Invokes a predicate on the value of the Either

PARAMETERS
Right
Func<R, bool>
Right predicate
Left
Func<L, bool>
Left predicate
DECLARATION
[Pure]
public bool BiExists(Func<R, bool> Right, Func<L, bool> Left)
RETURNS
bool
True if the predicate returns True. False otherwise or if the Either is in a bottom state.
BiFold<S>(S, Func<S, R, S>, Func<S, L, S>)
SUMMARY

Either 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:

PARAMETERS
state
S
Initial state
Right
Func<S, R, S>
Folder function, applied if Either is in a Right state
Left
Func<S, L, S>
Folder function, applied if Either is in a Left state
DECLARATION
[Pure]
public S BiFold<S>(S state, Func<S, R, S> Right, Func<S, L, S> Left)
RETURNS
S
The aggregate state
BiForAll(Func<R, bool>, Func<L, bool>)
SUMMARY

Invokes a predicate on the value of the Either if it's in the Right state

PARAMETERS
Right
Func<R, bool>
Predicate
Left
Func<L, bool>
Predicate
DECLARATION
[Pure]
public bool BiForAll(Func<R, bool> Right, Func<L, bool> Left)
RETURNS
bool
True if either Predicate returns true
BiIter(Action<R>, Action<L>)
SUMMARY

Iterate the Either action is invoked if in the Right state

PARAMETERS
Right
Action<R>
Left
Action<L>
DECLARATION
public Unit BiIter(Action<R> Right, Action<L> Left)
BiMap<L2, R2>(Func<R, R2>, Func<L, L2>)
SUMMARY

Bi-maps the value in the Either if it's in a Right state

PARAMETERS
Right
Func<R, R2>
Right map function
Left
Func<L, L2>
Left map function
DECLARATION
[Pure]
public Either<L2, R2> BiMap<L2, R2>(Func<R, R2> Right, Func<L, L2> Left)
RETURNS
Either<L2, R2>
Mapped Either
BiMap<LRet, Ret>(Func<R, Ret>, Func<L, Ret>)
SUMMARY

Bi-maps the value in the Either if it's in a Right state

PARAMETERS
Right
Func<R, Ret>
Right map function
Left
Func<L, Ret>
Left map function
DECLARATION
[Pure]
public Either<L, Ret> BiMap<LRet, Ret>(Func<R, Ret> Right, Func<L, Ret> Left)
RETURNS
Either<L, Ret>
Mapped Either
Bind<Ret>(Func<R, Either<L, Ret>>)
SUMMARY

Monadic bind

PARAMETERS
binder
Func<R, Either<L, Ret>>
DECLARATION
[Pure]
public Either<L, Ret> Bind<Ret>(Func<R, Either<L, Ret>> binder)
RETURNS
Either<L, Ret>
Bound Either
CompareTo(Either<L, R>)
SUMMARY

CompareTo override

PARAMETERS
other
Either<L, R>
DECLARATION
[Pure]
public int CompareTo(Either<L, R> other)
RETURNS
int
CompareTo(R)
SUMMARY

CompareTo override

PARAMETERS
other
R
DECLARATION
[Pure]
public int CompareTo(R other)
RETURNS
int
CompareTo(L)
SUMMARY

CompareTo override

PARAMETERS
other
L
DECLARATION
[Pure]
public int CompareTo(L other)
RETURNS
int
Count()
SUMMARY

Counts the Either

DECLARATION
[Pure]
public int Count()
RETURNS
int
1 if the Either is in a Right state, 0 otherwise.
Equals(object)
SUMMARY

Equality check

PARAMETERS
obj
object
Object to test for equality
DECLARATION
[Pure]
public override bool Equals(object obj)
RETURNS
bool
True if equal
Equals(R)
SUMMARY

Equality override

PARAMETERS
other
R
DECLARATION
[Pure]
public bool Equals(R other)
RETURNS
bool
Equals(L)
SUMMARY

Equality override

PARAMETERS
other
L
DECLARATION
[Pure]
public bool Equals(L other)
RETURNS
bool
Equals(Either<L, R>)
SUMMARY

Equality override

PARAMETERS
other
Either<L, R>
DECLARATION
[Pure]
public bool Equals(Either<L, R> other)
RETURNS
bool
Exists(Func<R, bool>)
SUMMARY

Invokes a predicate on the value of the Either if it's in the Right state

PARAMETERS
pred
Func<R, bool>
Predicate
DECLARATION
[Pure]
public bool Exists(Func<R, bool> pred)
RETURNS
bool
True if the Either is in a Right state and the predicate returns True. False otherwise.
Filter(Func<R, bool>)
SUMMARY

Filter the Either

PARAMETERS
pred
Func<R, bool>
Predicate function
REMARKS
This may give unpredictable results for a filtered value. The Either won't return true for IsLeft or IsRight. IsBottom is True if the value is filtered and that should be checked for.
DECLARATION
[Pure]
public Either<L, R> Filter(Func<R, bool> pred)
RETURNS
Either<L, R>
If the Either is in the Left state it is returned as-is. If in the Right state the predicate is applied to the Right value. If the predicate returns True the Either is returned as-is. If the predicate returns False the Either is returned in a 'Bottom' state.
Fold<S>(S, Func<S, R, S>)
SUMMARY

Either 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:

PARAMETERS
state
S
Initial state
Right
Func<S, R, S>
Folder function, applied if structure is in a Right state
DECLARATION
[Pure]
public S Fold<S>(S state, Func<S, R, S> Right)
RETURNS
S
The aggregate state
ForAll(Func<R, bool>)
SUMMARY

Invokes a predicate on the value of the Either if it's in the Right state

PARAMETERS
Right
Func<R, bool>
Predicate
DECLARATION
[Pure]
public bool ForAll(Func<R, bool> Right)
RETURNS
bool
True if the Either is in a Left state. True if the Either is in a Right state and the predicate returns True. False otherwise.
GetEnumerator()
DECLARATION
public IEnumerator<EitherData<L, R>> GetEnumerator()
RETURNS
IEnumerator<EitherData<L, R>>
GetHashCode()
SUMMARY

Returns a hash code of the wrapped value of the Either

DECLARATION
[Pure]
public override int GetHashCode()
RETURNS
int
Hash code
GetObjectData(SerializationInfo, StreamingContext)
PARAMETERS
info
SerializationInfo
context
StreamingContext
DECLARATION
public void GetObjectData(SerializationInfo info, StreamingContext context)
GetUnderlyingLeftType()
SUMMARY

Find out the underlying Left type

DECLARATION
[Pure]
public Type GetUnderlyingLeftType()
RETURNS
Type
GetUnderlyingRightType()
SUMMARY

Find out the underlying Right type

DECLARATION
[Pure]
public Type GetUnderlyingRightType()
RETURNS
Type
GetUnderlyingType()
DECLARATION
[Pure]
public Type GetUnderlyingType()
RETURNS
Type
IfLeft(Func<R>)
SUMMARY

Executes the Left function if the Either is in a Left state. Returns the Right value if the Either is in a Right state.

PARAMETERS
Left
Func<R>
Function to generate a Right value if in the Left state
DECLARATION
[Pure]
public R IfLeft(Func<R> Left)
RETURNS
R
Returns an unwrapped Right value
IfLeft(Func<L, R>)
SUMMARY

Executes the leftMap function if the Either is in a Left state. Returns the Right value if the Either is in a Right state.

PARAMETERS
leftMap
Func<L, R>
Function to generate a Right value if in the Left state
DECLARATION
[Pure]
public R IfLeft(Func<L, R> leftMap)
RETURNS
R
Returns an unwrapped Right value
IfLeft(R)
SUMMARY

Returns the rightValue if the Either is in a Left state. Returns the Right value if the Either is in a Right state.

PARAMETERS
rightValue
R
Value to return if in the Left state
DECLARATION
[Pure]
public R IfLeft(R rightValue)
RETURNS
R
Returns an unwrapped Right value
IfLeft(Action<L>)
SUMMARY

Executes the Left action if the Either is in a Left state.

PARAMETERS
Left
Action<L>
Function to generate a Right value if in the Left state
DECLARATION
public Unit IfLeft(Action<L> Left)
IfRight(Action<R>)
SUMMARY

Invokes the Right action if the Either is in a Right state, otherwise does nothing

PARAMETERS
Right
Action<R>
Action to invoke
DECLARATION
public Unit IfRight(Action<R> Right)
IfRight(L)
SUMMARY

Returns the leftValue if the Either is in a Right state. Returns the Left value if the Either is in a Left state.

PARAMETERS
leftValue
L
Value to return if in the Left state
DECLARATION
[Pure]
public L IfRight(L leftValue)
RETURNS
L
Returns an unwrapped Left value
IfRight(Func<L>)
SUMMARY

Returns the result of Right() if the Either is in a Right state. Returns the Left value if the Either is in a Left state.

PARAMETERS
Right
Func<L>
Function to generate a Left value if in the Right state
DECLARATION
[Pure]
public L IfRight(Func<L> Right)
RETURNS
L
Returns an unwrapped Left value
IfRight(Func<R, L>)
SUMMARY

Returns the result of rightMap if the Either is in a Right state. Returns the Left value if the Either is in a Left state.

PARAMETERS
rightMap
Func<R, L>
Function to generate a Left value if in the Right state
DECLARATION
[Pure]
public L IfRight(Func<R, L> rightMap)
RETURNS
L
Returns an unwrapped Left value
Iter(Action<R>)
SUMMARY

Iterate the Either action is invoked if in the Right state

PARAMETERS
Right
Action<R>
DECLARATION
public Unit Iter(Action<R> Right)
Join<U, K, V>(Either<L, U>, Func<R, K>, Func<U, K>, Func<R, U, V>)
PARAMETERS
inner
Either<L, U>
outerKeyMap
Func<R, K>
innerKeyMap
Func<U, K>
project
Func<R, U, V>
DECLARATION
[Pure]
public Either<L, V> Join<U, K, V>(Either<L, U> inner, Func<R, K> outerKeyMap, Func<U, K> innerKeyMap, Func<R, U, V> project)
RETURNS
Either<L, V>
LeftAsEnumerable()
SUMMARY

Project the Either into a IEnumerable L

DECLARATION
[Pure]
public Seq<L> LeftAsEnumerable()
RETURNS
Seq<L>
If the Either is in a Left state, a IEnumerable of L with one item. A zero length IEnumerable L otherwise
LeftToArray()
SUMMARY

Project the Either into an ImmutableArray R

DECLARATION
[Pure]
public Arr<L> LeftToArray()
RETURNS
Arr<L>
If the Either is in a Right state, a ImmutableArray of R with one item. A zero length ImmutableArray of R otherwise
LeftToList()
SUMMARY

Project the Either into a Lst R

DECLARATION
[Pure]
public Lst<L> LeftToList()
RETURNS
Lst<L>
If the Either is in a Right state, a Lst of R with one item. A zero length Lst R otherwise
LeftToSeq()
SUMMARY

Convert either to sequence of 0 or 1 right values

DECLARATION
[Pure]
public Seq<L> LeftToSeq()
RETURNS
Seq<L>
Map<Ret>(Func<R, Ret>)
SUMMARY

Maps the value in the Either if it's in a Right state

PARAMETERS
mapper
Func<R, Ret>
Map function
DECLARATION
[Pure]
public Either<L, Ret> Map<Ret>(Func<R, Ret> mapper)
RETURNS
Either<L, Ret>
Mapped Either
MapLeft<Ret>(Func<L, Ret>)
SUMMARY

Maps the value in the Either if it's in a Left state

PARAMETERS
mapper
Func<L, Ret>
Map function
DECLARATION
[Pure]
public Either<Ret, R> MapLeft<Ret>(Func<L, Ret> mapper)
RETURNS
Either<Ret, R>
Mapped Either
Match(Action<R>, Action<L>, Action)
SUMMARY

Invokes the Right or Left action depending on the state of the Either

PARAMETERS
Right
Action<R>
Action to invoke if in a Right state
Left
Action<L>
Action to invoke if in a Left state
Bottom
Action
DECLARATION
public Unit Match(Action<R> Right, Action<L> Left, Action Bottom = null)
EXCEPTIONS
BottomException
Thrown if matching on an Either in a bottom state
Match<Ret>(Func<R, Ret>, Func<L, Ret>, Func<Ret>)
SUMMARY

Invokes the Right or Left function depending on the state of the Either

PARAMETERS
Right
Func<R, Ret>
Function to invoke if in a Right state
Left
Func<L, Ret>
Function to invoke if in a Left state
Bottom
Func<Ret>
DECLARATION
[Pure]
public Ret Match<Ret>(Func<R, Ret> Right, Func<L, Ret> Left, Func<Ret> Bottom = null)
RETURNS
Ret
The return value of the invoked function
EXCEPTIONS
BottomException
Thrown if matching on an Either in a bottom state
MatchAsync<R2>(Func<R, Task<R2>>, Func<L, R2>)
SUMMARY

Match the two states of the Either and return a promise for a non-null R2.

PARAMETERS
Right
Func<R, Task<R2>>
Left
Func<L, R2>
DECLARATION
public Task<R2> MatchAsync<R2>(Func<R, Task<R2>> Right, Func<L, R2> Left)
RETURNS
Task<R2>
A promise to return a non-null R2
MatchAsync<R2>(Func<R, Task<R2>>, Func<L, Task<R2>>)
SUMMARY

Match the two states of the Either and return a promise for a non-null R2.

PARAMETERS
Right
Func<R, Task<R2>>
Left
Func<L, Task<R2>>
DECLARATION
public Task<R2> MatchAsync<R2>(Func<R, Task<R2>> Right, Func<L, Task<R2>> Left)
RETURNS
Task<R2>
A promise to return a non-null R2
MatchObservable<R2>(Func<R, IObservable<R2>>, Func<L, R2>)
SUMMARY

Match the two states of the Either and return an observable stream of non-null R2s.

PARAMETERS
Right
Func<R, IObservable<R2>>
Left
Func<L, R2>
DECLARATION
[Pure]
public IObservable<R2> MatchObservable<R2>(Func<R, IObservable<R2>> Right, Func<L, R2> Left)
RETURNS
IObservable<R2>
MatchObservable<R2>(Func<R, IObservable<R2>>, Func<L, IObservable<R2>>)
SUMMARY

Match the two states of the Either and return an observable stream of non-null R2s.

PARAMETERS
Right
Func<R, IObservable<R2>>
Left
Func<L, IObservable<R2>>
DECLARATION
[Pure]
public IObservable<R2> MatchObservable<R2>(Func<R, IObservable<R2>> Right, Func<L, IObservable<R2>> Left)
RETURNS
IObservable<R2>
MatchUnsafe<Ret>(Func<R, Ret>, Func<L, Ret>, Func<Ret>)
SUMMARY

Invokes the Right or Left function depending on the state of the Either

PARAMETERS
Right
Func<R, Ret>
Function to invoke if in a Right state
Left
Func<L, Ret>
Function to invoke if in a Left state
Bottom
Func<Ret>
DECLARATION
[Pure]
public Ret MatchUnsafe<Ret>(Func<R, Ret> Right, Func<L, Ret> Left, Func<Ret> Bottom = null)
RETURNS
Ret
The return value of the invoked function
EXCEPTIONS
BottomException
Thrown if matching on an Either in a bottom state
MatchUntyped<R1>(Func<object, R1>, Func<R1>)
PARAMETERS
Some
Func<object, R1>
None
Func<R1>
DECLARATION
[Pure]
public R1 MatchUntyped<R1>(Func<object, R1> Some, Func<R1> None)
RETURNS
R1
MatchUntyped<TResult>(Func<object, TResult>, Func<object, TResult>)
SUMMARY

Match the Right and Left values but as objects. This can be useful to avoid reflection.

PARAMETERS
Right
Func<object, TResult>
Left
Func<object, TResult>
DECLARATION
[Pure]
public TResult MatchUntyped<TResult>(Func<object, TResult> Right, Func<object, TResult> Left)
RETURNS
TResult
Right(Action<R>)
SUMMARY

Match Right and return a context. You must follow this with .Left(...) to complete the match

PARAMETERS
right
Action<R>
Action to invoke if the Either is in a Right state
DECLARATION
[Pure]
public EitherUnitContext<L, R> Right(Action<R> right)
RETURNS
EitherUnitContext<L, R>
Context that must have Left() called upon it.
Right<Ret>(Func<R, Ret>)
SUMMARY

Match Right and return a context. You must follow this with .Left(...) to complete the match

PARAMETERS
right
Func<R, Ret>
Action to invoke if the Either is in a Right state
DECLARATION
[Pure]
public EitherContext<L, R, Ret> Right<Ret>(Func<R, Ret> right)
RETURNS
EitherContext<L, R, Ret>
Context that must have Left() called upon it.
RightAsEnumerable()
SUMMARY

Project the Either into a IEnumerable R

DECLARATION
[Pure]
public Seq<R> RightAsEnumerable()
RETURNS
Seq<R>
If the Either is in a Right state, a IEnumerable of R with one item. A zero length IEnumerable R otherwise
RightToArray()
SUMMARY

Project the Either into an ImmutableArray R

DECLARATION
[Pure]
public Arr<R> RightToArray()
RETURNS
Arr<R>
If the Either is in a Right state, a ImmutableArray of R with one item. A zero length ImmutableArray of R otherwise
RightToList()
SUMMARY

Project the Either into a Lst R

DECLARATION
[Pure]
public Lst<R> RightToList()
RETURNS
Lst<R>
If the Either is in a Right state, a Lst of R with one item. A zero length Lst R otherwise
RightToSeq()
SUMMARY

Convert either to sequence of 0 or 1 right values

DECLARATION
[Pure]
public Seq<R> RightToSeq()
RETURNS
Seq<R>
Select<U>(Func<R, U>)
SUMMARY

Maps the value in the Either if it's in a Right state

PARAMETERS
map
Func<R, U>
Map function
DECLARATION
[Pure]
public Either<L, U> Select<U>(Func<R, U> map)
RETURNS
Either<L, U>
Mapped Either
SelectMany<U, V>(Func<R, Either<L, U>>, Func<R, U, V>)
SUMMARY

Monadic bind function

PARAMETERS
bind
Func<R, Either<L, U>>
project
Func<R, U, V>
DECLARATION
[Pure]
public Either<L, V> SelectMany<U, V>(Func<R, Either<L, U>> bind, Func<R, U, V> project)
RETURNS
Either<L, V>
Bound Either
ToArray()
SUMMARY

Project the Either into an ImmutableArray R

DECLARATION
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("ToArray has been deprecated. Please use RightToArray.")]
public Arr<R> ToArray()
RETURNS
Arr<R>
If the Either is in a Right state, a ImmutableArray of R with one item. A zero length ImmutableArray of R otherwise
ToEitherUnsafe()
SUMMARY

Convert the Either to an EitherUnsafe

DECLARATION
[Pure]
public EitherUnsafe<L, R> ToEitherUnsafe()
RETURNS
EitherUnsafe<L, R>
EitherUnsafe
ToList()
SUMMARY

Project the Either into a Lst R

DECLARATION
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("ToList has been deprecated. Please use RightToList.")]
public Lst<R> ToList()
RETURNS
Lst<R>
If the Either is in a Right state, a Lst of R with one item. A zero length Lst R otherwise
ToOption()
SUMMARY

Convert the Either to an Option

DECLARATION
[Pure]
public Option<R> ToOption()
RETURNS
Option<R>
Some(Right) or None
ToString()
SUMMARY

Return a string representation of the Either

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

Convert the Either to an TryOption

DECLARATION
[Pure]
public TryOption<R> ToTryOption()
RETURNS
TryOption<R>
Some(Right) or None
ToValidation()
DECLARATION
[Pure]
public Validation<L, R> ToValidation()
RETURNS
Validation<L, R>
Where(Func<R, bool>)
SUMMARY

Filter the Either

PARAMETERS
pred
Func<R, bool>
Predicate function
REMARKS
This may give unpredictable results for a filtered value. The Either won't return true for IsLeft or IsRight. IsBottom is True if the value is filtered and that should be checked for.
DECLARATION
[Pure]
[EditorBrowsable(EditorBrowsableState.Never)]
public Either<L, R> Where(Func<R, bool> pred)
RETURNS
Either<L, R>
If the Either is in the Left state it is returned as-is. If in the Right state the predicate is applied to the Right value. If the predicate returns True the Either is returned as-is. If the predicate returns False the Either is returned in a 'Bottom' state. IsLeft will return True, but the value of Left = default(L)