LanguageExt.Core

LanguageExt.Core Traits Monads Monad

Contents

class MonadExtensions Source #

Methods

method K<M, B> Bind <M, A, B> ( this K<M, A> ma, Func<A, K<M, B>> f) Source #

where M : Monad<M>

Monad bind operation

Parameters

type A

Initial bound value type

type B

Intermediate bound value type

param f

Monadic bind function

returns

M〈B〉

method K<M, B> Bind <M, A, B> ( this K<M, A> ma, Func<A, Pure<B>> f) Source #

where M : Functor<M>

Monad bind operation

Parameters

type A

Initial bound value type

type B

Intermediate bound value type

param f

Monadic bind function

returns

M〈B〉

method K<M, C> SelectMany <M, A, B, C> ( this K<M, A> ma, Func<A, K<M, B>> bind, Func<A, B, C> project) Source #

where M : Monad<M>

Monad bind operation

Parameters

type A

Initial bound value type

type B

Intermediate bound value type

type C

Target bound value type

param bind

Monadic bind function

param project

Projection function

returns

M〈C〉

method K<M, C> SelectMany <M, A, B, C> ( this K<M, A> ma, Func<A, Pure<B>> bind, Func<A, B, C> project) Source #

where M : Functor<M>

Monad bind operation

Parameters

type A

Initial bound value type

type B

Intermediate bound value type

type C

Target bound value type

param bind

Monadic bind function

param project

Projection function

returns

M〈C〉

method K<M, C> SelectMany <E, M, A, C> ( this K<M, A> ma, Func<A, Guard<E, Unit>> bind, Func<A, Unit, C> project) Source #

where M : Monad<M>, Fallible<E, M>

Monad bind operation

Parameters

type M

Monad trait

type A

Initial bound value type

type C

Target bound value type

param bind

Monadic bind function

param project

Projection function

returns

M〈C〉

method K<M, C> SelectMany <E, M, A, C> ( this K<M, A> ma, Func<A, Guard<Fail<E>, Unit>> bind, Func<A, Unit, C> project) Source #

where M : Monad<M>, Fallible<E, M>

Monad bind operation

Parameters

type M

Monad trait

type A

Initial bound value type

type C

Target bound value type

param bind

Monadic bind function

param project

Projection function

returns

M〈C〉

method K<M, A> Flatten <M, A> (this K<M, K<M, A>> mma) Source #

where M : Monad<M>

Monadic join operation

Parameters

type M

Monad trait

type A

Bound value type

param mma
returns

Joined monad

class MonadExtensions Source #

Monad module

Methods

method K<M, K<N, B>> BindT <M, N, A, B> (this K<M, K<N, A>> mna, Func<A, K<N, B>> f) Source #

where M : Functor<M>
where N : Monad<N>

Runs a monadic bind operation on the nested monads

If you're working with an inner monad that is concrete then you will first need to call KindT to cast the monad to a more general K version. This enables the T variant extensions (like BindT, `MapT, etc.) to work without providing excessive generic arguments all the way down the chain.

Parameters

type M

Outer monad trait

type N

Inner monad trait

type A

Input bound value

type B

Output bound value

param mna

Nested monadic value

param f

Bind function

returns

Mapped value

Examples

var mx = Seq〈Option〈int〉〉(Some(1), Some(2), Some(3));

var ma = mx.MapT(a => a + 1);

method K<M, K<N, B>> BindT <M, N, A, B> (this K<M, K<N, A>> mna, Func<A, K<M, K<N, B>>> f) Source #

where M : Monad<M>
where N : Monad<N>, Traversable<N>

Runs a monadic bind operation on the nested monads

If you're working with an inner monad that is concrete then you will first need to call KindT to cast the monad to a more general K version. This enables the T variant extensions (like BindT, `MapT, etc.) to work without providing excessive generic arguments all the way down the chain.

Parameters

type M

Outer monad trait

type N

Inner monad trait

type A

Input bound value

type B

Output bound value

param mna

Nested monadic value

param f

Bind function

returns

Mapped value

Examples

var mx = Seq〈Option〈int〉〉(Some(1), Some(2), Some(3));

var ma = mx.BindT(a => Seq(Some(a + 1))) .MapT(a => a + 1);;

class MonadLaw <F> Source #

where F : Monad<F>

Functions that test that monad laws hold for the F monad provided.

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

Parameters

type F

Functor type

Methods

method Unit assert (Func<K<F, int>, K<F, int>, bool>? equals = null) Source #

Assert that the monad laws hold

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

method Validation<Error, Unit> validate (Func<K<F, int>, K<F, int>, bool>? equals = null) Source #

Validate that the monad laws hold

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

method Validation<Error, Unit> recurIsSameAsBind (Func<K<F, int>, K<F, int>, bool>? equals = null) Source #

method Validation<Error, Unit> leftIdentityLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Validate the left-identity law

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

method Validation<Error, Unit> rightIdentityLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Validate the right-identity law

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

method Validation<Error, Unit> associativityLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Validate the associativity law

NOTE: Equals must be implemented for the K〈F, *〉 derived-type, so that the laws can be proven to be true. If your monad doesn't have Equals then you must provide the optional equals parameter so that the equality of outcomes can be tested.

class Monad Source #

Monad module

Methods

method K<M, A> pure <M, A> (A value) Source #

where M : Monad<M>

method K<M, A> flatten <M, A> (K<M, K<M, A>> mma) Source #

where M : Monad<M>

method K<M, B> bind <M, A, B> (K<M, A> ma, Func<A, K<M, B>> f) Source #

where M : Monad<M>

method MB bind <M, MB, A, B> (K<M, A> ma, Func<A, MB> f) Source #

where MB : K<M, B>
where M : Monad<M>

method K<M, Next<A, B>> done <M, A, B> (B value) Source #

where M : Monad<M>

Lift a Next.Done value into the monad

method K<M, Next<A, B>> loop <M, A, B> (A value) Source #

where M : Monad<M>

Lift a Next.Done value into the monad

method K<M, B> recur <M, A, B> (A value, Func<A, K<M, Next<A, B>>> f) Source #

where M : Monad<M>

Allow for tail-recursion by using a trampoline function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to keep the computation going or not.

It is expected that the implementor of the Monad trait has made a 'stack-neutral' implementation of Monad.Recur

Parameters

type M

Monad type

type A

Loop value

type B

Done value

param value

Initial value to start the recursive process

param f

Bind function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to recur, or not.

returns

Monad structure

method K<M, B> unsafeRecur <M, A, B> (A value, Func<A, K<M, Next<A, B>>> f) Source #

where M : Monad<M>

This is a default implementation of Monad.Recur that doesn't use the trampoline. It's here to use as a placeholder implementation for the trampoline version and for types where it's unlikely that recursion will be a problem. NOTE: Using this isn't declarative, and the users of Monad.recur would rightly be miffed if an implementation yielded a stack-overflow, so use this function with caution.

Parameters

type M

Monad type

type A

Loop value

type B

Done value

param value

Initial value to start the recursive process

param f

Bind function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to recur, or not.

returns

Monad structure

method K<M, B> iterableRecur <M, A, B> (A value, Func<A, K<M, Next<A, B>>> f) Source #

where M : Natural<M, Iterable>, CoNatural<M, Iterable>

Allow for tail-recursion by using a trampoline function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to keep the computation going or not.

This is a handy pre-built version of Monad.Recur that works with Iterable (a lazy stream that supports both synchronicity and asynchronicity). The Natural and CoNatural constraints allow any type that can convert to and from Iterable to gain this prebuilt stack-protecting recursion.

Parameters

type M

Monad type

type A

Loop value

type B

Done value

param value

Initial value to start the recursive process

param f

Bind function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to recur, or not.

returns

Monad structure

method IEnumerable<B> enumerableRecur <A, B> (A value, Func<A, IEnumerable<Next<A, B>>> f) Source #

Allow for tail-recursion by using a trampoline function that returns an enumerable monad with the bound value wrapped by Next, which enables decision-making about whether to keep the computation going or not.

This is a handy pre-built version of Monad.Recur that works with IEnumerable (a lazy stream that supports synchronicity only)

Parameters

type M

Monad type

type A

Loop value

type B

Done value

param value

Initial value to start the recursive process

param f

Bind function that returns a monad with the bound value wrapped by Next, which enables decision-making about whether to recur, or not.

returns

Monad structure

method K<M, Unit> when <M> (K<M, bool> Pred, K<M, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to true, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> when <M> (K<M, bool> Pred, Pure<Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to true, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> unless <M> (K<M, bool> Pred, K<M, Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to false, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, Unit> unless <M> (K<M, bool> Pred, Pure<Unit> Then) Source #

where M : Monad<M>

When the predicate evaluates to false, compute Then

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<M, A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<M, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, Pure<A> Then, K<IO, A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

method K<M, A> iff <M, A> (K<M, bool> Pred, K<IO, A> Then, Pure<A> Else) Source #

where M : Monad<M>

Compute the predicate and depending on its state compute Then or Else

Parameters

type M

Monad

param Pred

Predicate

param Then

Computation

param Else

Computation

returns

Unit monad

class MonadExtensions Source #

Operators

operator >> (K<M, A> ma, Func<A, K<M, B>> f) Source #

Monad bind operator

Parameters

param ma

Monad to bind

param f

Binding function

returns

Mapped monad

operator >> (K<M, A> lhs, K<M, B> rhs) Source #

Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in C#.

Parameters

param lhs

First action to run

param rhs

Second action to run

returns

Result of the second action

operator << (K<M, A> lhs, K<M, B> rhs) Source #

Sequentially compose two actions, discarding any value produced by the second, like sequencing operators (such as the semicolon) in C#.

Parameters

param lhs

First action to run

param mb

Second action to run

returns

Result of the second action

interface Monad <M> Source #

where M : Monad<M>

Monad trait

Parameters

type M

Self-referring trait

Methods

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

method K<M, B> Recur <A, B> (A value, Func<A, K<M, Next<A, B>>> f) Source #

Tail-recursive bind

Parameters

type A

Continuation value type

type B

Completed value type

param value

Initial value to the bind expression

param f

Bind function

returns

Result of repeatedly invoking f until Right(b) is returned

method K<M, Next<A, B>> Done <A, B> (B value) Source #

Lift a Next.Done value into the monad

method K<M, Next<A, B>> Loop <A, B> (A value) Source #

Lift a Next.Done value into the monad

method K<M, C> SelectMany <A, B, C> (K<M, A> ma, Func<A, K<M, B>> bind, Func<A, B, C> project) Source #

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

method K<M, A> Flatten <A> (K<M, K<M, A>> mma) Source #