LanguageExt.Core

LanguageExt.Core Monads Monadic conditionals

guard, when, and unless allow for conditional operations and short-cutting in monadic expressions.

Guards

Guards are used to stop the monadic expression continuing if a flag is true (for guard) or false (for guardnot).

They only work with monads that have an 'alternative value' (which is usually used as the error condition: Left in Either for example). An alternative value is provided when the guard triggers:

from x in ma
from _ in guard(x == 100, Error.New("x should be 100"))
select x;

Supported monads are:

Either
EitherUnsafe
EitherAsync
Fin
Validation
Aff
Eff

When and Unless

when and unless are similar to guards, but instead of providing the alternative value, you provide an alternative monad to run. This monad could be in a failed state, or it could run a successful side effect (an Aff calling Console<RT>.writeLine() for example).

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

Contents

class Prelude Source #

Methods

method Guard<E> guard <E> (bool flag, Func<E> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<E> guard <E> (bool flag, E False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error> guard (bool flag, Func<Error> False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<Error> guard (bool flag, Error False) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param False

If the flag is false, this provides the error

returns

Guard

method Guard<E> guardnot <E> (bool flag, Func<E> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<E> guardnot <E> (bool flag, E True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error> guardnot (bool flag, Func<Error> True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

method Guard<Error> guardnot (bool flag, Error True) Source #

Guard against continuing a monadic expression

Parameters

param flag

Flag for continuing

param True

If the flag is false, this provides the error

returns

Guard

class Prelude Source #

Methods

method Aff<RT, Unit> unless <RT> (bool flag, Aff<RT, Unit> alternative) Source #

where RT : struct, HasCancel<RT>

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method Eff<RT, Unit> unless <RT> (bool flag, Eff<RT, Unit> alternative) Source #

where RT : struct, HasCancel<RT>

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method Aff<Unit> unless (bool flag, Aff<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method Eff<Unit> unless (bool flag, Eff<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Console.writeLine<RT>("x should be 100!"))
select x;

method Try<Unit> unless (bool flag, Try<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Try(() => WriteLineAsync<RT>("x should be 100!")))
select x;

method TryAsync<Unit> unless (bool flag, TryAsync<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, TryAsync(() => WriteLineAsync<RT>("x should be 100!")))
select x;

method TryOption<Unit> unless (bool flag, TryOption<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, TryOption(() => WriteLineAsync<RT>("x should be 100!")))
select x;

method TryOptionAsync<Unit> unless (bool flag, TryOptionAsync<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x should be 100!")))
select x;

method Option<Unit> unless (bool flag, Option<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, None)
select x;

method OptionUnsafe<Unit> unless (bool flag, OptionUnsafe<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, None)
select x;

method OptionAsync<Unit> unless (bool flag, OptionAsync<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, None)
select x;

method Either<L, Unit> unless <L> (bool flag, Either<L, Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Left<string, int>("100 is the only value accepted"))
select x;

method EitherUnsafe<L, Unit> unless <L> (bool flag, EitherUnsafe<L, Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, LeftUnsafe<string, int>("100 is the only value accepted"))
select x;

method EitherAsync<L, Unit> unless <L> (bool flag, EitherAsync<L, Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, LeftAsync<string, int>("100 is the only value accepted"))
select x;

method Fin<Unit> unless (bool flag, Fin<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, FinFail(Error.New("100 is the only value accepted"))
select x;

method Task<Unit> unless (bool flag, Task<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, async () => await LaunchMissiles())
select x;

method ValueTask<Unit> unless (bool flag, ValueTask<Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, async () => await LaunchMissiles())
select x;

method Validation<L, Unit> unless <L> (bool flag, Validation<L, Unit> alternative) Source #

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Fail<string, Unit>("100 is the only value accepted"))
select x;

method Validation<MonoidL, L, Unit> unless <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative) Source #

where MonoidL : struct, Monoid<L>, Eq<L>

Run the alternative when the flag is false, return Pure Unit when true

Parameters

param flag

If false the alternative is run

param alternative

Computation to run if the flag is false

returns

Either the result of the alternative computation if the flag is false or Unit

Examples

from x in ma
from _ in unless(x == 100, Fail<MString, string, Unit>("100 is the only value accepted"))
select x;

class Prelude Source #

Methods

method Aff<RT, Unit> when <RT> (bool flag, Aff<RT, Unit> alternative) Source #

where RT : struct, HasCancel<RT>

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method Eff<RT, Unit> when <RT> (bool flag, Eff<RT, Unit> alternative) Source #

where RT : struct, HasCancel<RT>

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method Aff<Unit> when (bool flag, Aff<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method Eff<Unit> when (bool flag, Eff<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Console.writeLine<RT>("x is 100, finally!"))
select x;

method Try<Unit> when (bool flag, Try<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Try(() => WriteLine<RT>("x is 100, finally!")))
select x;

method TryAsync<Unit> when (bool flag, TryAsync<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, TryAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;

method TryOption<Unit> when (bool flag, TryOption<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;

method TryOptionAsync<Unit> when (bool flag, TryOptionAsync<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, TryOptionAsync(() => WriteLineAsync<RT>("x is 100, finally!")))
select x;

method Option<Unit> when (bool flag, Option<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, None)
select x;

method OptionUnsafe<Unit> when (bool flag, OptionUnsafe<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, None)
select x;

method OptionAsync<Unit> when (bool flag, OptionAsync<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, None)
select x;

method Either<L, Unit> when <L> (bool flag, Either<L, Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Left<string, int>("Won't accept 100"))
select x;

method EitherUnsafe<L, Unit> when <L> (bool flag, EitherUnsafe<L, Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, LeftUnsafe<string, int>("Won't accept 100"))
select x;

method EitherAsync<L, Unit> when <L> (bool flag, EitherAsync<L, Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, LeftAsync<string, int>("Won't accept 100"))
select x;

method Fin<Unit> when (bool flag, Fin<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, FinFail(Error.New("Won't accept 100")))
select x;

method Task<Unit> when (bool flag, Task<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, async () => await LaunchMissiles())
select x;

method ValueTask<Unit> when (bool flag, ValueTask<Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, async () => await LaunchMissiles())
select x;

method Validation<L, Unit> when <L> (bool flag, Validation<L, Unit> alternative) Source #

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Fail<string, Unit>("100 isn't accepted"))
select x;

method Validation<MonoidL, L, Unit> when <MonoidL, L> (bool flag, Validation<MonoidL, L, Unit> alternative) Source #

where MonoidL : struct, Monoid<L>, Eq<L>

Run the alternative when the flag is true, return Pure Unit when false

Parameters

param flag

If true the alternative is run

param alternative

Computation to run if the flag is true

returns

Either the result of the alternative computation if the flag is true or Unit

Examples

from x in ma
from _ in when(x == 100, Fail<MString, string, Unit>("100 isn't accepted"))
select x;