LanguageExt.Core

LanguageExt.Core Traits Alternative

Alternative<F> allows for propagation of 'failure' and 'choice' (in some appropriate sense, depending on the type), as well as provision of a unit/identity value (Empty).

Alternative is a Choice and MonoidK, which means it has a Choose method, a Combine method (which defaults to calling the Choose method), and an Empty method. That creates a semantic meaning for Choose, which is about choice propagation rather than the broader meaning of SemigroupK.Combine. It also allows for Choose and Combine to have separate implementations depending on the type.

The way to think about Choose and the inherited SemigroupK.Combine methods is:

Any type that supports the Alternative trait should also implement the | operator, to enable easy choice/failure propagation. If there is a different implementation of Combine (rather than accepting the default), then the type should also implement the + operator.

AlternativeLaw can help you test your implementation:

choose(Pure(a), Pure(b)) = Pure(a)
choose(Empty  , Pure(b)) = Pure(b)
choose(Pure(a), Empty  ) = Pure(a)
choose(Empty  , Empty  ) = Empty

It also tests the Applicative and Functor laws.

Contents

class AlternativeExtensions Source #

A monoid on applicative functors

Parameters

type F

Applicative functor

Methods

method K<F, A> OneOf <F, A> (this Seq<K<F, A>> ms) Source #

where F : Alternative<F>

Given a set of applicative functors, return the first one to succeed.

If none succeed, the last applicative functor will be returned.

class AlternativeLaw <F> Source #

where F : Alternative<F>

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

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

Parameters

type F

Alternative type

Methods

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

Assert that the Alternative 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 Alternative structure 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 Alternative 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 Alternative structure 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> leftZeroLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Left-zero law

choose(empty, pure(x)) = pure(x)

NOTE: Equals must be implemented for the K<F, *> derived-type, so that the laws can be proven to be true. If your Alternative structure 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> rightZeroLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Right-zero law

choose(pure(x), empty) = pure(x)

NOTE: Equals must be implemented for the K<F, *> derived-type, so that the laws can be proven to be true. If your Alternative structure 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> leftCatchLaw (Func<K<F, int>, K<F, int>, bool> equals) Source #

Left catch law

choose(pure(x), pure(y)) = pure(x)

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

class Alternative Source #

Methods

method K<F, A> oneOf <F, A> (params K<F, A>[] ms) Source #

where F : Alternative<F>

Given a set of applicative functors, return the first one to succeed.

If none succeed, the last applicative functor will be returned.

method K<F, A> oneOf <F, A> (Seq<K<F, A>> ms) Source #

where F : Alternative<F>

Given a set of applicative functors, return the first one to succeed.

If none succeed, the last applicative functor will be returned.

method K<F, Seq<A>> some <F, A> (K<F, A> fa) Source #

where F : Alternative<F>, Applicative<F>

One or more...

Run the applicative functor repeatedly, collecting the results, until failure.

Will always succeed if at least one item has been yielded.

NOTE: It is important that the F applicative-type overrides Apply (the one with Func laziness) in its trait-implementations otherwise this will likely result in a stack-overflow.

Parameters

param fa

Applicative functor

returns

One or more values

method K<F, Seq<A>> many <F, A> (K<F, A> fa) Source #

where F : Alternative<F>, Applicative<F>

Zero or more...

Run the applicative functor repeatedly, collecting the results, until failure. Will always succeed.

NOTE: It is important that the F applicative-type overrides ApplyLazy in its trait-implementations otherwise this will likely result in a stack-overflow.

Parameters

param fa

Applicative functor

returns

Zero or more values

class Prelude Source #

Methods

method K<F, A> oneOf <F, A> (params K<F, A>[] ms) Source #

where F : Alternative<F>

Given a set of applicative functors, return the first one to succeed.

If none succeed, the last applicative functor will be returned.

method K<F, A> oneOf <F, A> (Seq<K<F, A>> ms) Source #

where F : Alternative<F>

Given a set of applicative functors, return the first one to succeed.

If none succeed, the last applicative functor will be returned.

interface Alternative <F> Source #

where F : Alternative<F>