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 Object
implements MonadT<M,A,StateT<S,M,?>,StateT<S,?,?>>, MonadReader<S,A,StateT<S,M,?>>, MonadWriter<S,A,StateT<S,M,?>>
The
State monad transformer.- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUpdate the accumulated state.discardL(Applicative<B, StateT<S, M, ?>> appB) Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.discardR(Applicative<B, StateT<S, M, ?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.Run the stateful computation embedded in theMonad, returning the result.Run the stateful computation embedded in theMonad, returning the final state.Chain dependent computations that may continue or short-circuit based on previous results.Covariantly transmute this functor's parameter using the given mapping function.Given a function that produces a value inside amonadic effectfrom a state, produce aStateTthat simply passes its state to the function and applies it.Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Map the accumulation into a value and pair it with the current output.Modify thisMonadReader'senvironment after reading it but before running the effect.Map both the result and the final state to a new result and final state inside theMonad.pure(B b) Lift the valuebinto this applicative functor.pureStateT(Pure<M> pureM) Lift amonadic stateintoStateT.Lift a state-sensitivemonadically embeddedcomputation intoStateT.Lift amonadic valueintoStateT.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.Map the final state to a new final state inside the samemonadic effectusing the provided 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
-
stateFn
-
-
Constructor Details
-
StateT
-
-
Method Details
-
runStateT
-
evalT
Run the stateful computation embedded in theMonad, returning the result.- Type Parameters:
MA- the inferredMonadresult- Parameters:
s- the initial state- Returns:
- the result
-
execT
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>, StateT<S,B> N, mapStateTB> (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
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
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
Update the accumulated state.- Specified by:
censorin interfaceMonadWriter<S,M extends MonadRec<?, M>, A> - Parameters:
fn- the update function- Returns:
- the updated
MonadWriter
-
local
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
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
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
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
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, lazyZipB>> (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:
-
discardL
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
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
- 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, trampolineMB> (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.
-
get
-
gets
public static <S,M extends MonadRec<?, StateT<S,M>, A> M, getsA> (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
-
put
Lift amonadic stateintoStateT. -
stateT
Lift amonadic valueintoStateT. -
stateT
public static <S,M extends MonadRec<?, StateT<S,M>, A> M, stateTA> (Fn1<? super S, ? extends MonadRec<Tuple2<A, S>, M>> stateFn) Lift a state-sensitivemonadically embeddedcomputation intoStateT. -
pureStateT
-
liftStateT
-