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,java.lang.Object,java.lang.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 java.lang.Object implements MonadRec<B,Const<A,?>>, Bifunctor<A,B,Const<?,?>>, Traversable<B,Const<A,?>>
A (surprisingly useful) functor over some phantom typeB
, retaining a value of typeA
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 howB
has been altered,A
is still pristine and retrievable.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <C,D>
Const<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.<Z> Const<Z,B>
biMapL(Fn1<? super A,? extends Z> fn)
Covariantly map over the left parameter.<C> Const<A,C>
biMapR(Fn1<? super B,? extends C> fn)
Covariantly map over the right parameter.<C> Const<A,C>
discardL(Applicative<C,Const<A,?>> appB)
Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.<C> Const<A,B>
discardR(Applicative<C,Const<A,?>> appB)
Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.boolean
equals(java.lang.Object other)
<C> Const<A,C>
flatMap(Fn1<? super B,? extends Monad<C,Const<A,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.<C> Const<A,C>
fmap(Fn1<? super B,? extends C> fn)
Map over the right parameter.int
hashCode()
<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.<C> Const<A,C>
pure(C c)
Lift the valueb
into this applicative functor.static <A> Pure<Const<A,?>>
pureConst(A a)
A
runConst()
Retrieve the stored value.java.lang.String
toString()
<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.<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.<C> Const<A,C>
zip(Applicative<Fn1<? super B,? extends C>,Const<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.
-
-
-
Field Detail
-
a
private final A a
-
-
Constructor Detail
-
Const
public Const(A a)
-
-
Method Detail
-
runConst
public A runConst()
Retrieve the stored value.- Returns:
- the value
-
fmap
public <C> Const<A,C> fmap(Fn1<? super B,? extends C> fn)
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)
-
zip
public <C> Const<A,C> zip(Applicative<Fn1<? super B,? extends C>,Const<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.
-
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:
Maybe
,Either
-
discardL
public <C> Const<A,C> discardL(Applicative<C,Const<A,?>> appB)
Sequence both thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
. This is generally useful for sequentially performing side-effects.
-
discardR
public <C> Const<A,B> discardR(Applicative<C,Const<A,?>> appB)
Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
. This is generally useful for sequentially performing side-effects.
-
flatMap
public <C> Const<A,C> flatMap(Fn1<? super B,? extends Monad<C,Const<A,?>>> f)
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:
for a basic implementation
,for a implementation
,for an implementation leveraging an already stack-safe
,for a implementation
-
traverse
public <C,App extends Applicative<?,App>,TravB extends Traversable<C,Const<A,?>>,AppTrav extends Applicative<TravB,App>> AppTrav traverse(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
public <Z> Const<Z,B> biMapL(Fn1<? super A,? extends Z> fn)
Covariantly map over the left parameter.- Specified by:
biMapL
in interfaceBifunctor<A,B,Const<?,?>>
- Specified by:
biMapL
in interfaceBoundedBifunctor<A,B,java.lang.Object,java.lang.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
public <C> Const<A,C> biMapR(Fn1<? super B,? extends C> fn)
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,java.lang.Object,java.lang.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
public <C,D> Const<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:
biMap
in interfaceBifunctor<A,B,Const<?,?>>
- Specified by:
biMap
in interfaceBoundedBifunctor<A,B,java.lang.Object,java.lang.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
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-