Class Choice2<A,B>
- Type Parameters:
A- the first possible typeB- the second possible type
- All Implemented Interfaces:
CoProduct2<A,,B, Choice2<A, B>> Applicative<B,,Choice2<A, ?>> Bifunctor<A,,B, Choice2<?, ?>> BoundedBifunctor<A,,B, Object, Object, Choice2<?, ?>> Functor<B,,Choice2<A, ?>> Monad<B,,Choice2<A, ?>> MonadRec<B,,Choice2<A, ?>> Traversable<B,Choice2<A, ?>>
- Direct Known Subclasses:
Choice2._A,Choice2._B
CoProduct2. Unlike Either, there is no concept of "success" or
"failure", so the domain of reasonable function semantics is more limited.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classChoice2._A<A,B> private static final classChoice2._B<A,B> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A,B> Choice2 <A, B> a(A a) Static factory method for wrapping a value of typeAin aChoice2.static <A,B> Choice2 <A, B> b(B b) Static factory method for wrapping a value of typeBin aChoice2.final <C,D> Choice2 <C, D> Dually map covariantly over both the left and right parameters.Covariantly map over the left parameter.Covariantly map over the right parameter.discardL(Applicative<C, Choice2<A, ?>> appB) Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.discardR(Applicative<C, Choice2<A, ?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.diverge()Diverge this coproduct by introducing another possible type that it could represent.Chain dependent computations that may continue or short-circuit based on previous results.Covariantly transmute this functor's parameter using the given mapping function.invert()Swap the type parameters.Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.project()Specialize this choice's projection to aTuple2.pure(C c) Lift the valuebinto this applicative functor.trampolineM(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, Choice2<A, ?>>> fn) Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.<C,App extends Applicative<?, App>, TravB extends Traversable<C, Choice2<A, ?>>, AppTrav extends Applicative<TravB, App>>
AppTravtraverse(Fn1<? super B, ? extends Applicative<C, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Applyfnto 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.Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.jnape.palatable.lambda.adt.coproduct.CoProduct2
embed, match, projectA, projectB
-
Constructor Details
-
Choice2
private Choice2()
-
-
Method Details
-
project
Specialize this choice's projection to aTuple2. -
diverge
Diverge this coproduct by introducing another possible type that it could represent. As no morphisms can be provided mapping current types to the new type, this operation merely acts as a convenience method to allow the use of a more convergent coproduct with a more divergent one; that is, if aCoProduct3<String, Integer, Boolean>is expected, aCoProduct2<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
CoProduct2could not be aCoProduct3without specifying all type parameters that are possible for aCoProduct3- more specifically, the third possible type - which is not necessarily known at construction time, or even useful if never used in the context of aCoProduct3. The inverse inheritance relationship -CoProduct3<CoProduct2- is inherently unsound, as aCoProduct3cannot correctly implementCoProduct2.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 typeCis always possible.For this reason, there is a
divergemethod supported between allCoProducttypes of single magnitude difference.- Specified by:
divergein interfaceCoProduct2<A,B, Choice2<A, B>> - Type Parameters:
C- the additional possible type of this coproduct- Returns:
- a
CoProduct3<A, B, C>
-
invert
Swap the type parameters. -
fmap
Covariantly transmute this functor's parameter using the given mapping function. Generally this method is specialized to return an instance of the class implementing Functor.- Specified by:
fmapin interfaceApplicative<A,B> - Specified by:
fmapin interfaceFunctor<A,B> - Specified by:
fmapin interfaceMonad<A,B> - Specified by:
fmapin interfaceMonadRec<A,B> - Specified by:
fmapin interfaceTraversable<A,B> - Type Parameters:
C- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
biMapL
Covariantly map over the left parameter.- Specified by:
biMapLin interfaceBifunctor<A,B, Choice2<?, ?>> - Specified by:
biMapLin interfaceBoundedBifunctor<A,B, Object, Object, Choice2<?, ?>> - Type Parameters:
C- the new left parameter type- Parameters:
fn- the mapping function- Returns:
- a bifunctor over C (the new left parameter) and B (the same right parameter)
-
biMapR
Covariantly map over the right parameter. For all bifunctors that are also functors, it should hold thatbiMapR(f) == fmap(f).- Specified by:
biMapRin interfaceBifunctor<A,B, Choice2<?, ?>> - Specified by:
biMapRin interfaceBoundedBifunctor<A,B, Object, Object, Choice2<?, ?>> - Type Parameters:
C- the new right parameter type- Parameters:
fn- the mapping function- Returns:
- a bifunctor over A (the same left parameter) and C (the new right parameter)
-
biMap
public final <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. This is isomorphic tobiMapL(lFn).biMapR(rFn).- Specified by:
biMapin interfaceBifunctor<A,B, Choice2<?, ?>> - Specified by:
biMapin interfaceBoundedBifunctor<A,B, Object, Object, Choice2<?, ?>> - Type Parameters:
C- the new left parameter typeD- the new right parameter type- Parameters:
lFn- the left parameter mapping functionrFn- the right parameter mapping function- Returns:
- a bifunctor over C (the new left parameter type) and D (the new right parameter type)
-
pure
Lift the valuebinto this applicative functor. -
zip
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports. -
lazyZip
public <C> Lazy<Choice2<A,C>> lazyZip(Lazy<? extends Applicative<Fn1<? super B, ? extends C>, Choice2<A, ?>>> lazyAppFn) Given alazyinstance 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.- Specified by:
lazyZipin interfaceApplicative<A,B> - Specified by:
lazyZipin interfaceMonad<A,B> - Specified by:
lazyZipin interfaceMonadRec<A,B> - Type Parameters:
C- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
-
discardL
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects. -
discardR
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects. -
flatMap
Chain dependent computations that may continue or short-circuit based on previous results. -
trampolineM
public <C> Choice2<A,C> trampolineM(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, Choice2<A, ?>>> fn) Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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.
- Specified by:
trampolineMin interfaceMonadRec<A,B> - Type Parameters:
C- the ultimate resulting carrier type- Parameters:
fn- the function to internally trampoline- Returns:
- the trampolined
MonadRec - See Also:
-
traverse
public <C,App extends Applicative<?, AppTrav traverseApp>, TravB extends Traversable<C, Choice2<A, ?>>, AppTrav extends Applicative<TravB, App>> (Fn1<? super B, ? extends Applicative<C, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Applyfnto 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.- Specified by:
traversein interfaceTraversable<A,B> - Type Parameters:
C- the resulting element typeApp- the result applicative typeTravB- this Traversable instance over BAppTrav- the full inferred resulting type from the traversal- Parameters:
fn- the function to applypure- the applicative pure function- Returns:
- the traversed Traversable, wrapped inside an applicative
-
a
Static factory method for wrapping a value of typeAin aChoice2.- Type Parameters:
A- the first possible typeB- the second possible type- Parameters:
a- the value- Returns:
- the wrapped value as a
Choice2<A, B>
-
b
Static factory method for wrapping a value of typeBin aChoice2.- Type Parameters:
A- the first possible typeB- the second possible type- Parameters:
b- the value- Returns:
- the wrapped value as a
Choice2<A, B>
-
pureChoice
- Type Parameters:
A- the first possible type- Returns:
- the
Pureinstance
-