A
- the first possible typeB
- the second possible typepublic abstract class Choice2<A,B> extends Object implements CoProduct2<A,B,Choice2<A,B>>, MonadRec<B,Choice2<A,?>>, Bifunctor<A,B,Choice2<?,?>>, Traversable<B,Choice2<A,?>>
CoProduct2
. Unlike Either
, there is no concept of "success" or
"failure", so the domain of reasonable function semantics is more limited.Modifier and Type | Method and Description |
---|---|
static <A,B> Choice2<A,B> |
a(A a)
Static factory method for wrapping a value of type
A in a Choice2 . |
static <A,B> Choice2<A,B> |
b(B b)
Static factory method for wrapping a value of type
B in a Choice2 . |
<C,D> Choice2<C,D> |
biMap(Fn1<? super A,? extends C> lFn,
Fn1<? super B,? extends D> rFn)
Dually map covariantly over both the left and right parameters.
|
<C> Choice2<C,B> |
biMapL(Fn1<? super A,? extends C> fn)
Covariantly map over the left parameter.
|
<C> Choice2<A,C> |
biMapR(Fn1<? super B,? extends C> fn)
Covariantly map over the right parameter.
|
<C> Choice2<A,C> |
discardL(Applicative<C,Choice2<A,?>> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
<C> Choice2<A,B> |
discardR(Applicative<C,Choice2<A,?>> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
<C> Choice3<A,B,C> |
diverge()
Diverge this coproduct by introducing another possible type that it could represent.
|
<C> Choice2<A,C> |
flatMap(Fn1<? super B,? extends Monad<C,Choice2<A,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.
|
<C> Choice2<A,C> |
fmap(Fn1<? super B,? extends C> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
Choice2<B,A> |
invert()
Swap the type parameters.
|
<C> Lazy<Choice2<A,C>> |
lazyZip(Lazy<? extends Applicative<Fn1<? super B,? extends C>,Choice2<A,?>>> lazyAppFn)
Given a
lazy instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. |
Tuple2<Maybe<A>,Maybe<B>> |
project()
Specialize this choice's projection to a
Tuple2 . |
<C> Choice2<A,C> |
pure(C c)
Lift the value
b into this applicative functor. |
static <A> Pure<Choice2<A,?>> |
pureChoice()
|
<C> Choice2<A,C> |
trampolineM(Fn1<? super B,? extends MonadRec<RecursiveResult<B,C>,Choice2<A,?>>> fn)
Given some operation yielding a
RecursiveResult inside this MonadRec , internally trampoline the
operation until it yields a termination instruction. |
<C,App extends Applicative<?,App>,TravB extends Traversable<C,Choice2<A,?>>,AppTrav extends Applicative<TravB,App>> |
traverse(Fn1<? super B,? extends Applicative<C,App>> fn,
Fn1<? super TravB,? extends AppTrav> pure)
Apply
fn to each element of this traversable from left to right, and collapse the results into
a single resulting applicative, potentially with the assistance of the applicative's pure function. |
<C> Choice2<A,C> |
zip(Applicative<Fn1<? super B,? extends C>,Choice2<A,?>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using
whatever application semantics the current applicative supports.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
embed, match, projectA, projectB
public final <C> Choice3<A,B,C> diverge()
CoProduct3<String, Integer,
Boolean>
is expected, a CoProduct2<String, Integer>
should suffice.
Generally, we use inheritance to make this a non-issue; however, with coproducts of differing magnitudes, we
cannot guarantee variance compatibility in one direction conveniently at construction time, and in the other
direction, at all. A CoProduct2
could not be a CoProduct3
without specifying all type parameters
that are possible for a CoProduct3
- more specifically, the third possible type - which is not
necessarily known at construction time, or even useful if never used in the context of a CoProduct3
. The
inverse inheritance relationship - CoProduct3
< CoProduct2
- is inherently unsound, as a
CoProduct3
cannot correctly implement CoProduct2.match(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends R>, com.jnape.palatable.lambda.functions.Fn1<? super B, ? extends R>)
, given that the third type C
is always possible.
For this reason, there is a diverge
method supported between all CoProduct
types of
single magnitude difference.
diverge
in interface CoProduct2<A,B,Choice2<A,B>>
C
- the additional possible type of this coproductCoProduct3
<A, B, C>public final <C> Choice2<A,C> fmap(Fn1<? super B,? extends C> fn)
fmap
in interface Applicative<B,Choice2<A,?>>
fmap
in interface Functor<B,Choice2<A,?>>
fmap
in interface Monad<B,Choice2<A,?>>
fmap
in interface MonadRec<B,Choice2<A,?>>
fmap
in interface Traversable<B,Choice2<A,?>>
C
- the new parameter typefn
- the mapping functionpublic final <C> Choice2<C,B> biMapL(Fn1<? super A,? extends C> fn)
biMapL
in interface Bifunctor<A,B,Choice2<?,?>>
biMapL
in interface BoundedBifunctor<A,B,Object,Object,Choice2<?,?>>
C
- the new left parameter typefn
- the mapping functionpublic final <C> Choice2<A,C> biMapR(Fn1<? super B,? extends C> fn)
biMapR(f) == fmap(f)
.biMapR
in interface Bifunctor<A,B,Choice2<?,?>>
biMapR
in interface BoundedBifunctor<A,B,Object,Object,Choice2<?,?>>
C
- the new right parameter typefn
- the mapping functionpublic final <C,D> Choice2<C,D> biMap(Fn1<? super A,? extends C> lFn, Fn1<? super B,? extends D> rFn)
biMapL(lFn).biMapR(rFn)
.biMap
in interface Bifunctor<A,B,Choice2<?,?>>
biMap
in interface BoundedBifunctor<A,B,Object,Object,Choice2<?,?>>
C
- the new left parameter typeD
- the new right parameter typelFn
- the left parameter mapping functionrFn
- the right parameter mapping functionpublic <C> Choice2<A,C> pure(C c)
b
into this applicative functor.pure
in interface Applicative<B,Choice2<A,?>>
pure
in interface Monad<B,Choice2<A,?>>
pure
in interface MonadRec<B,Choice2<A,?>>
C
- the type of the returned applicative's parameterc
- the valuepublic <C> Choice2<A,C> zip(Applicative<Fn1<? super B,? extends C>,Choice2<A,?>> appFn)
zip
in interface Applicative<B,Choice2<A,?>>
zip
in interface Monad<B,Choice2<A,?>>
zip
in interface MonadRec<B,Choice2<A,?>>
C
- the resulting applicative parameter typeappFn
- the other applicative instancepublic <C> Lazy<Choice2<A,C>> lazyZip(Lazy<? extends Applicative<Fn1<? super B,? extends C>,Choice2<A,?>>> lazyAppFn)
lazy
instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. This is useful for applicatives that
support lazy evaluation and early termination.lazyZip
in interface Applicative<B,Choice2<A,?>>
lazyZip
in interface Monad<B,Choice2<A,?>>
lazyZip
in interface MonadRec<B,Choice2<A,?>>
C
- the resulting applicative parameter typelazyAppFn
- the lazy other applicative instanceMaybe
,
Either
public <C> Choice2<A,C> discardL(Applicative<C,Choice2<A,?>> appB)
Applicative
and appB
, discarding this Applicative's
result and returning appB
. This is generally useful for sequentially performing side-effects.discardL
in interface Applicative<B,Choice2<A,?>>
discardL
in interface Monad<B,Choice2<A,?>>
discardL
in interface MonadRec<B,Choice2<A,?>>
C
- the type of the returned Applicative's parameterappB
- the other Applicativepublic <C> Choice2<A,B> discardR(Applicative<C,Choice2<A,?>> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.public final <C> Choice2<A,C> flatMap(Fn1<? super B,? extends Monad<C,Choice2<A,?>>> f)
public <C> Choice2<A,C> trampolineM(Fn1<? super B,? extends MonadRec<RecursiveResult<B,C>,Choice2<A,?>>> fn)
RecursiveResult
inside this MonadRec
, internally trampoline the
operation until it yields a termination
instruction.
Stack-safety depends on implementations guaranteeing that the growth of the call stack is a constant factor independent of the number of invocations of the operation. For various examples of how this can be achieved in stereotypical circumstances, see the referenced types.
trampolineM
in interface MonadRec<B,Choice2<A,?>>
C
- the ultimate resulting carrier typefn
- the function to internally trampolineMonadRec
for a basic implementation
,
for a {@link CoProduct2 coproduct} implementation
,
for an implementation leveraging an already stack-safe {@link Monad#flatMap(Fn1)}
,
for a {@link MonadT monad transformer} implementation
public <C,App extends Applicative<?,App>,TravB extends Traversable<C,Choice2<A,?>>,AppTrav extends Applicative<TravB,App>> AppTrav traverse(Fn1<? super B,? extends Applicative<C,App>> fn, Fn1<? super TravB,? extends AppTrav> pure)
fn
to each element of this traversable from left to right, and collapse the results into
a single resulting applicative, potentially with the assistance of the applicative's pure function.traverse
in interface Traversable<B,Choice2<A,?>>
C
- the resulting element typeApp
- the result applicative typeTravB
- this Traversable instance over BAppTrav
- the full inferred resulting type from the traversalfn
- the function to applypure
- the applicative pure functionpublic static <A,B> Choice2<A,B> a(A a)
A
in a Choice2
.A
- the first possible typeB
- the second possible typea
- the valueChoice2
<A, B>public static <A,B> Choice2<A,B> b(B b)
B
in a Choice2
.A
- the first possible typeB
- the second possible typeb
- the valueChoice2
<A, B>