Class StateT<S,M extends MonadRec<?,M>,A>
- java.lang.Object
-
- com.jnape.palatable.lambda.monad.transformer.builtin.StateT<S,M,A>
-
- Type Parameters:
S- the state typeM- themonadic embeddingA- the result type
- All Implemented Interfaces:
Applicative<A,StateT<S,M,?>>,Functor<A,StateT<S,M,?>>,Monad<A,StateT<S,M,?>>,MonadBase<M,A,StateT<S,?,?>>,MonadReader<S,A,StateT<S,M,?>>,MonadRec<A,StateT<S,M,?>>,MonadWriter<S,A,StateT<S,M,?>>,MonadT<M,A,StateT<S,M,?>,StateT<S,?,?>>
public final class StateT<S,M extends MonadRec<?,M>,A> extends java.lang.Object implements MonadT<M,A,StateT<S,M,?>,StateT<S,?,?>>, MonadReader<S,A,StateT<S,M,?>>, MonadWriter<S,A,StateT<S,M,?>>
TheStatemonad transformer.- See Also:
State
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StateT<S,M,A>censor(Fn1<? super S,? extends S> fn)Update the accumulated state.<B> StateT<S,M,B>discardL(Applicative<B,StateT<S,M,?>> appB)Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<B> StateT<S,M,A>discardR(Applicative<B,StateT<S,M,?>> appB)Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.<MA extends Monad<A,M>>
MAevalT(S s)Run the stateful computation embedded in theMonad, returning the result.<MS extends Monad<S,M>>
MSexecT(S s)Run the stateful computation embedded in theMonad, returning the final state.<B> StateT<S,M,B>flatMap(Fn1<? super A,? extends Monad<B,StateT<S,M,?>>> f)Chain dependent computations that may continue or short-circuit based on previous results.<B> StateT<S,M,B>fmap(Fn1<? super A,? extends B> fn)Covariantly transmute this functor's parameter using the given mapping function.static <A,M extends MonadRec<?,M>>
StateT<A,M,A>get(Pure<M> pureM)static <S,M extends MonadRec<?,M>,A>
StateT<S,M,A>gets(Fn1<? super S,? extends MonadRec<A,M>> fn)Given a function that produces a value inside amonadic effectfrom a state, produce aStateTthat simply passes its state to the function and applies it.<B> Lazy<StateT<S,M,B>>lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,StateT<S,M,?>>> lazyAppFn)Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.<B,N extends MonadRec<?,N>>
StateT<S,N,B>lift(MonadRec<B,N> mb)static <S> Lift<StateT<S,?,?>>liftStateT()<B> StateT<S,M,Tuple2<A,B>>listens(Fn1<? super S,? extends B> fn)Map the accumulation into a value and pair it with the current output.StateT<S,M,A>local(Fn1<? super S,? extends S> fn)Modify thisMonadReader'senvironment after reading it but before running the effect.<N extends MonadRec<?,N>,B>
StateT<S,N,B>mapStateT(Fn1<? super MonadRec<Tuple2<A,S>,M>,? extends MonadRec<Tuple2<B,S>,N>> fn)Map both the result and the final state to a new result and final state inside theMonad.static <S,M extends MonadRec<?,M>>
StateT<S,M,Unit>modify(Fn1<? super S,? extends MonadRec<S,M>> updateFn)<B> StateT<S,M,B>pure(B b)Lift the valuebinto this applicative functor.static <S,M extends MonadRec<?,M>>
Pure<StateT<S,M,?>>pureStateT(Pure<M> pureM)static <S,M extends MonadRec<?,M>>
StateT<S,M,Unit>put(MonadRec<S,M> ms)Lift amonadic stateintoStateT.<MAS extends MonadRec<Tuple2<A,S>,M>>
MASrunStateT(S s)static <S,M extends MonadRec<?,M>,A>
StateT<S,M,A>stateT(Fn1<? super S,? extends MonadRec<Tuple2<A,S>,M>> stateFn)Lift a state-sensitivemonadically embeddedcomputation intoStateT.static <S,M extends MonadRec<?,M>,A>
StateT<S,M,A>stateT(MonadRec<A,M> ma)Lift amonadic valueintoStateT.<B> StateT<S,M,B>trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,StateT<S,M,?>>> fn)Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.StateT<S,M,A>withStateT(Fn1<? super S,? extends MonadRec<S,M>> fn)Map the final state to a new final state inside the samemonadic effectusing the provided function.<B> StateT<S,M,B>zip(Applicative<Fn1<? super A,? extends B>,StateT<S,M,?>> appFn)Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
-
-
-
Method Detail
-
evalT
public <MA extends Monad<A,M>> MA evalT(S s)
Run the stateful computation embedded in theMonad, returning the result.- Type Parameters:
MA- the inferredMonadresult- Parameters:
s- the initial state- Returns:
- the result
-
execT
public <MS extends Monad<S,M>> MS execT(S s)
Run the stateful computation embedded in theMonad, returning the final state.- Type Parameters:
MS- the inferredMonadresult- Parameters:
s- the initial state- Returns:
- the final state
-
mapStateT
public <N extends MonadRec<?,N>,B> StateT<S,N,B> mapStateT(Fn1<? super MonadRec<Tuple2<A,S>,M>,? extends MonadRec<Tuple2<B,S>,N>> fn)
Map both the result and the final state to a new result and final state inside theMonad.- Type Parameters:
N- the newmonadic embeddingfor thisStateTB- the new state type- Parameters:
fn- the mapping function- Returns:
- the mapped
StateT
-
withStateT
public StateT<S,M,A> withStateT(Fn1<? super S,? extends MonadRec<S,M>> fn)
Map the final state to a new final state inside the samemonadic effectusing the provided function.- Parameters:
fn- the state-mapping function- Returns:
- the mapped
StateT
-
listens
public <B> StateT<S,M,Tuple2<A,B>> listens(Fn1<? super S,? extends B> fn)
Map the accumulation into a value and pair it with the current output.- Specified by:
listensin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the mapped output- Parameters:
fn- the mapping function- Returns:
- the updated
MonadWriter
-
censor
public StateT<S,M,A> censor(Fn1<? super S,? extends S> fn)
Update the accumulated state.- Specified by:
censorin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Parameters:
fn- the update function- Returns:
- the updated
MonadWriter
-
local
public StateT<S,M,A> local(Fn1<? super S,? extends S> fn)
Modify thisMonadReader'senvironment after reading it but before running the effect.- Specified by:
localin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Parameters:
fn- the modification function- Returns:
- the
MonadReaderwith a modified environment
-
flatMap
public <B> StateT<S,M,B> flatMap(Fn1<? super A,? extends Monad<B,StateT<S,M,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
flatMapin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
flatMapin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
flatMapin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
flatMapin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the resulting monad parameter type- Parameters:
f- the dependent computation over A- Returns:
- the new monad instance
-
pure
public <B> StateT<S,M,B> pure(B b)
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<S,M extends MonadRec<?,M>>- Specified by:
purein interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
purein interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
purein interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
purein interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
purein interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the type of the returned applicative's parameter- Parameters:
b- the value- Returns:
- an instance of this applicative over b
-
fmap
public <B> StateT<S,M,B> fmap(Fn1<? super A,? extends B> fn)
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<S,M extends MonadRec<?,M>>- Specified by:
fmapin interfaceFunctor<S,M extends MonadRec<?,M>>- Specified by:
fmapin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
fmapin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
fmapin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
fmapin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
fmapin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
public <B> StateT<S,M,B> zip(Applicative<Fn1<? super A,? extends B>,StateT<S,M,?>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.- Specified by:
zipin interfaceApplicative<S,M extends MonadRec<?,M>>- Specified by:
zipin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
zipin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
zipin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
zipin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
zipin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
appFn- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<StateT<S,M,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,StateT<S,M,?>>> 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<S,M extends MonadRec<?,M>>- Specified by:
lazyZipin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
lazyZipin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
lazyZipin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
lazyZipin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
lazyZipin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
Maybe,Either
-
discardL
public <B> StateT<S,M,B> discardL(Applicative<B,StateT<S,M,?>> appB)
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<S,M extends MonadRec<?,M>>- Specified by:
discardLin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
discardLin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
discardLin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
discardLin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
discardLin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
discardR
public <B> StateT<S,M,A> discardR(Applicative<B,StateT<S,M,?>> appB)
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<S,M extends MonadRec<?,M>>- Specified by:
discardRin interfaceMonad<S,M extends MonadRec<?,M>>- Specified by:
discardRin interfaceMonadReader<S,M extends MonadRec<?,M>,A>- Specified by:
discardRin interfaceMonadRec<S,M extends MonadRec<?,M>>- Specified by:
discardRin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Specified by:
discardRin interfaceMonadWriter<S,M extends MonadRec<?,M>,A>- Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
lift
public <B,N extends MonadRec<?,N>> StateT<S,N,B> lift(MonadRec<B,N> mb)
- Specified by:
liftin interfaceMonadBase<S,M extends MonadRec<?,M>,A>- Specified by:
liftin interfaceMonadT<M extends MonadRec<?,M>,A,StateT<S,M extends MonadRec<?,M>,?>,StateT<S,?,?>>- Type Parameters:
B- theMonadReccarrier typeN- the argumentMonadRecwitness- Parameters:
mb- the argumentMonadRec- Returns:
- the new
MonadBase
-
trampolineM
public <B> StateT<S,M,B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,StateT<S,M,?>>> 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<S,M extends MonadRec<?,M>>- Type Parameters:
B- 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
-
gets
public static <S,M extends MonadRec<?,M>,A> StateT<S,M,A> gets(Fn1<? super S,? extends MonadRec<A,M>> fn)
Given a function that produces a value inside amonadic effectfrom a state, produce aStateTthat simply passes its state to the function and applies it.
-
modify
public static <S,M extends MonadRec<?,M>> StateT<S,M,Unit> modify(Fn1<? super S,? extends MonadRec<S,M>> updateFn)
-
put
public static <S,M extends MonadRec<?,M>> StateT<S,M,Unit> put(MonadRec<S,M> ms)
Lift amonadic stateintoStateT.
-
stateT
public static <S,M extends MonadRec<?,M>,A> StateT<S,M,A> stateT(MonadRec<A,M> ma)
Lift amonadic valueintoStateT.
-
stateT
public static <S,M extends MonadRec<?,M>,A> StateT<S,M,A> stateT(Fn1<? super S,? extends MonadRec<Tuple2<A,S>,M>> stateFn)
Lift a state-sensitivemonadically embeddedcomputation intoStateT.
-
pureStateT
public static <S,M extends MonadRec<?,M>> Pure<StateT<S,M,?>> pureStateT(Pure<M> pureM)
-
-