Class RecursiveResult<A,B>
java.lang.Object
com.jnape.palatable.lambda.functions.recursion.RecursiveResult<A,B>
- Type Parameters:
A- the recursive function's input typeB- the recursive function's output type
- All Implemented Interfaces:
CoProduct2<A, B, RecursiveResult<A,B>>, Applicative<B, RecursiveResult<A, ?>>, Bifunctor<A, B, RecursiveResult<?, ?>>, BoundedBifunctor<A, B, Object, Object, RecursiveResult<?, ?>>, Functor<B, RecursiveResult<A, ?>>, Monad<B, RecursiveResult<A, ?>>, MonadRec<B, RecursiveResult<A, ?>>, Traversable<B, RecursiveResult<A, ?>>
- Direct Known Subclasses:
RecursiveResult.Recurse, RecursiveResult.Terminate
public abstract class RecursiveResult<A,B>
extends Object
implements CoProduct2<A, B, RecursiveResult<A,B>>, Bifunctor<A, B, RecursiveResult<?,?>>, MonadRec<B, RecursiveResult<A,?>>, Traversable<B, RecursiveResult<A,?>>
Specialized
CoProduct2 representing the possible results of a primitive recursive function.
Used by Trampoline to cheat around CoProduct2.match(Fn1, Fn1) and quickly unpack values via
instanceof checks to package private inner subtypes.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class(package private) static final class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<C,D> RecursiveResult <C, D> Dually map covariantly over both the left and right parameters.<C> RecursiveResult<C, B> Covariantly map over the left parameter.<C> RecursiveResult<A, C> Covariantly map over the right parameter.<C> RecursiveResult<A, C> discardL(Applicative<C, RecursiveResult<A, ?>> appB) Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<C> RecursiveResult<A, B> discardR(Applicative<C, RecursiveResult<A, ?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.<C> RecursiveResult<A, C> Chain dependent computations that may continue or short-circuit based on previous results.<C> RecursiveResult<A, C> Covariantly transmute this functor's parameter using the given mapping function.invert()Swap the type parameters.<C> RecursiveResult<A, C> pure(C c) Lift the valuebinto this applicative functor.static <A> Pure<RecursiveResult<A, ?>> The canonicalPureinstance forRecursiveResult.static <A,B> RecursiveResult <A, B> recurse(A a) Static factory method for creating a "recurse" value.static <A,B> RecursiveResult <A, B> terminate(B b) Static factory method for creating a "terminate" value.<C> RecursiveResult<A, C> trampolineM(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, RecursiveResult<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, RecursiveResult<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.<C> RecursiveResult<A, C> zip(Applicative<Fn1<? super B, ? extends C>, RecursiveResult<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.
-
Constructor Details
-
RecursiveResult
private RecursiveResult()
-
-
Method Details
-
invert
Swap the type parameters.- Specified by:
invertin interfaceCoProduct2<A, B, RecursiveResult<A,B>> - Returns:
- The inverted coproduct
-
biMapL
Covariantly map over the left parameter.- Specified by:
biMapLin interfaceBifunctor<A, B, RecursiveResult<?,?>> - Specified by:
biMapLin interfaceBoundedBifunctor<A, B, Object, Object, RecursiveResult<?,?>> - 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, RecursiveResult<?,?>> - Specified by:
biMapRin interfaceBoundedBifunctor<A, B, Object, Object, RecursiveResult<?,?>> - 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 <C,D> RecursiveResult<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, RecursiveResult<?,?>> - Specified by:
biMapin interfaceBoundedBifunctor<A, B, Object, Object, RecursiveResult<?,?>> - 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)
-
flatMap
Chain dependent computations that may continue or short-circuit based on previous results. -
pure
Lift the valuebinto this applicative functor. -
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)
-
zip
public <C> RecursiveResult<A,C> zip(Applicative<Fn1<? super B, ? extends C>, RecursiveResult<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. -
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. -
trampolineM
public <C> RecursiveResult<A,C> trampolineM(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, RecursiveResult<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<?,App>, TravB extends Traversable<C, RecursiveResult<A, AppTrav traverse?>>, 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
-
recurse
Static factory method for creating a "recurse" value.- Type Parameters:
A- the recurse typeB- the terminate type- Parameters:
a- the value- Returns:
- the
RecursiveResult
-
terminate
Static factory method for creating a "terminate" value.- Type Parameters:
A- the recurse typeB- the terminate type- Parameters:
b- the value- Returns:
- the
RecursiveResult
-
pureRecursiveResult
The canonicalPureinstance forRecursiveResult.- Type Parameters:
A- the recursive function's input type- Returns:
- the
Pureinstance
-