Class Const<A,B>
java.lang.Object
com.jnape.palatable.lambda.functor.builtin.Const<A,B>
- Type Parameters:
A
- the left parameter type, and the type of the stored valueB
- the right (phantom) parameter type
- All Implemented Interfaces:
Applicative<B,
,Const<A, ?>> Bifunctor<A,
,B, Const<?, ?>> BoundedBifunctor<A,
,B, Object, Object, Const<?, ?>> Functor<B,
,Const<A, ?>> Monad<B,
,Const<A, ?>> MonadRec<B,
,Const<A, ?>> Traversable<B,
Const<A, ?>>
public final class Const<A,B>
extends Object
implements MonadRec<B,Const<A,?>>, Bifunctor<A,B,Const<?,?>>, Traversable<B,Const<A,?>>
A (surprisingly useful) functor over some phantom type
B
, retaining a value of type A
that
can be retrieved later. This is useful in situations where it is desirable to retain constant information throughout
arbitrary functor transformations, such that at the end of the chain, regardless of how B
has been
altered, A
is still pristine and retrievable.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<C,
D> Const <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, Const<A, ?>> appB) Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.discardR
(Applicative<C, Const<A, ?>> appB) Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.boolean
Chain dependent computations that may continue or short-circuit based on previous results.Map over the right parameter.int
hashCode()
Given alazy
instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.pure
(C c) Lift the valueb
into this applicative functor.pureConst
(A a) runConst()
Retrieve the stored value.toString()
trampolineM
(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, Const<A, ?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
instruction.<C,
App extends Applicative<?, App>, TravB extends Traversable<C, Const<A, ?>>, AppTrav extends Applicative<TravB, App>>
AppTravtraverse
(Fn1<? super B, ? extends Applicative<C, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Applyfn
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.Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
-
Field Details
-
a
-
-
Constructor Details
-
Const
-
-
Method Details
-
runConst
Retrieve the stored value.- Returns:
- the value
-
fmap
Map over the right parameter. Note that becauseB
is never actually known quantity outside of a type signature, this is effectively a no-op that serves only to alterConst's
type signature.- Specified by:
fmap
in interfaceApplicative<A,
B> - Specified by:
fmap
in interfaceFunctor<A,
B> - Specified by:
fmap
in interfaceMonad<A,
B> - Specified by:
fmap
in interfaceMonadRec<A,
B> - Specified by:
fmap
in interfaceTraversable<A,
B> - Type Parameters:
C
- the new right parameter type- Parameters:
fn
- the mapping function- Returns:
- a Const over A (the same value) and C (the new phantom parameter)
-
pure
Lift the valueb
into 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<Const<A,C>> lazyZip(Lazy<? extends Applicative<Fn1<? super B, ? extends C>, Const<A, ?>>> lazyAppFn) Given alazy
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.- Specified by:
lazyZip
in interfaceApplicative<A,
B> - Specified by:
lazyZip
in interfaceMonad<A,
B> - Specified by:
lazyZip
in 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 thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
. This is generally useful for sequentially performing side-effects. -
discardR
Sequence both thisApplicative
andappB
, discardingappB's
result 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> Const<A,C> trampolineM(Fn1<? super B, ? extends MonadRec<RecursiveResult<B, C>, Const<A, ?>>> fn) Given some operation yielding aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
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.
- Specified by:
trampolineM
in 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, Const<A, ?>>, AppTrav extends Applicative<TravB, App>> (Fn1<? super B, ? extends Applicative<C, App>> fn, Fn1<? super TravB, ? extends AppTrav> pure) Applyfn
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.- Specified by:
traverse
in 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
-
biMapL
Covariantly map over the left parameter.- Specified by:
biMapL
in interfaceBifunctor<A,
B, Const<?, ?>> - Specified by:
biMapL
in interfaceBoundedBifunctor<A,
B, Object, Object, Const<?, ?>> - Type Parameters:
Z
- 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:
biMapR
in interfaceBifunctor<A,
B, Const<?, ?>> - Specified by:
biMapR
in interfaceBoundedBifunctor<A,
B, Object, Object, Const<?, ?>> - 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
Dually map covariantly over both the left and right parameters. This is isomorphic tobiMapL(lFn).biMapR(rFn)
.- Specified by:
biMap
in interfaceBifunctor<A,
B, Const<?, ?>> - Specified by:
biMap
in interfaceBoundedBifunctor<A,
B, Object, Object, Const<?, ?>> - 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)
-
equals
-
hashCode
public int hashCode() -
toString
-
pureConst
- Type Parameters:
A
- the left parameter type, and the type of the stored value- Parameters:
a
- the stored value- Returns:
- the
Pure
instance
-