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:
Chooseis the failure/choice propagation operator:|Combineis the concatenation/combination/addition operator:+
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
- AlternativeExtensions
- AlternativeLaw <F>
- assert (Func<K<F, int>, K<F, int>, bool>? equals = null)
- validate (Func<K<F, int>, K<F, int>, bool>? equals = null)
- leftZeroLaw (Func<K<F, int>, K<F, int>, bool> equals)
- rightZeroLaw (Func<K<F, int>, K<F, int>, bool> equals)
- leftCatchLaw (Func<K<F, int>, K<F, int>, bool> equals)
- Alternative
- oneOf <F, A> (params K<F, A>[] ms)
- oneOf <F, A> (Seq<K<F, A>> ms)
- some <F, A> (K<F, A> fa)
- many <F, A> (K<F, A> fa)
- Prelude
- Alternative <F>
class AlternativeExtensions Source #
A monoid on applicative functors
Parameters
| type | F | Applicative functor |
class AlternativeLaw <F> Source #
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 #
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 #
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 #
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 #
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 | |