Class State<S,A>
- java.lang.Object
-
- com.jnape.palatable.lambda.functor.builtin.State<S,A>
-
- Type Parameters:
S- the state typeA- the result type
- All Implemented Interfaces:
Applicative<A,State<S,?>>,Functor<A,State<S,?>>,Monad<A,State<S,?>>,MonadReader<S,A,State<S,?>>,MonadRec<A,State<S,?>>,MonadWriter<S,A,State<S,?>>
public final class State<S,A> extends java.lang.Object implements MonadRec<A,State<S,?>>, MonadReader<S,A,State<S,?>>, MonadWriter<S,A,State<S,?>>
The stateMonad, useful for iteratively building up state and state-contextualized result.For more information, read about the state monad.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description State<S,A>censor(Fn1<? super S,? extends S> fn)Update the accumulated state.<B> State<S,B>discardL(Applicative<B,State<S,?>> appB)Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<B> State<S,A>discardR(Applicative<B,State<S,?>> appB)Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.Aeval(S s)Run the stateful computation, returning the result.Sexec(S s)Run the stateful computation, returning the final state.<B> State<S,B>flatMap(Fn1<? super A,? extends Monad<B,State<S,?>>> f)Chain dependent computations that may continue or short-circuit based on previous results.<B> State<S,B>fmap(Fn1<? super A,? extends B> fn)Covariantly transmute this functor's parameter using the given mapping function.static <A> State<A,A>get()Create aStatethat simply returns back the initial state as both the result and the final statestatic <S,A>
State<S,A>gets(Fn1<? super S,? extends A> fn)Create aStatethat maps its initial state into its result, but leaves the initial state unchanged.<B> Lazy<State<S,B>>lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,State<S,?>>> lazyAppFn)Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.<B> State<S,Tuple2<A,B>>listens(Fn1<? super S,? extends B> fn)Map the accumulation into a value and pair it with the current output.State<S,A>local(Fn1<? super S,? extends S> fn)Modify thisMonadReader'senvironment after reading it but before running the effect.<B> State<S,B>mapState(Fn1<? super Tuple2<A,S>,? extends Tuple2<B,S>> fn)Map both the result and the final state to a new result and final state.static <S> State<S,Unit>modify(Fn1<? super S,? extends S> fn)<B> State<S,B>pure(B b)Lift the valuebinto this applicative functor.static <S> Pure<State<S,?>>pureState()static <S> State<S,Unit>put(S s)Tuple2<A,S>run(S s)Run the stateful computation, returning aTuple2of the result and the final state.static <S,A>
State<S,A>state(A a)Create aStatethat returnsaas its result and its initial state as its final state.static <S,A>
State<S,A>state(Fn1<? super S,? extends Tuple2<A,S>> stateFn)Create aStatefromstateFn, a function that maps an initial state into a result and a final state.<B> State<S,B>trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,State<S,?>>> fn)Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.State<S,A>withState(Fn1<? super S,? extends S> fn)Map the final state to a new final state using the provided function.<B> State<S,B>zip(Applicative<Fn1<? super A,? extends B>,State<S,?>> 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
-
run
public Tuple2<A,S> run(S s)
Run the stateful computation, returning aTuple2of the result and the final state.- Parameters:
s- the initial state- Returns:
- a
Tuple2of the result and the final state.
-
eval
public A eval(S s)
Run the stateful computation, returning the result.- Parameters:
s- the initial state- Returns:
- the result
-
exec
public S exec(S s)
Run the stateful computation, returning the final state.- Parameters:
s- the initial state- Returns:
- the final state
-
mapState
public <B> State<S,B> mapState(Fn1<? super Tuple2<A,S>,? extends Tuple2<B,S>> fn)
Map both the result and the final state to a new result and final state.- Type Parameters:
B- the new state type- Parameters:
fn- the mapping function- Returns:
- the mapped
State
-
withState
public State<S,A> withState(Fn1<? super S,? extends S> fn)
Map the final state to a new final state using the provided function.- Parameters:
fn- the state-mapping function- Returns:
- the mapped
State
-
local
public State<S,A> local(Fn1<? super S,? extends S> fn)
Modify thisMonadReader'senvironment after reading it but before running the effect.- Specified by:
localin interfaceMonadReader<S,A,State<S,?>>- Parameters:
fn- the modification function- Returns:
- the
MonadReaderwith a modified environment
-
listens
public <B> State<S,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,A,State<S,?>>- Type Parameters:
B- the mapped output- Parameters:
fn- the mapping function- Returns:
- the updated
MonadWriter
-
censor
public State<S,A> censor(Fn1<? super S,? extends S> fn)
Update the accumulated state.- Specified by:
censorin interfaceMonadWriter<S,A,State<S,?>>- Parameters:
fn- the update function- Returns:
- the updated
MonadWriter
-
flatMap
public <B> State<S,B> flatMap(Fn1<? super A,? extends Monad<B,State<S,?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<S,A>- Specified by:
flatMapin interfaceMonadReader<S,A,State<S,?>>- Specified by:
flatMapin interfaceMonadRec<S,A>- Specified by:
flatMapin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the resulting monad parameter type- Parameters:
f- the dependent computation over A- Returns:
- the new monad instance
-
pure
public <B> State<S,B> pure(B b)
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<S,A>- Specified by:
purein interfaceMonad<S,A>- Specified by:
purein interfaceMonadReader<S,A,State<S,?>>- Specified by:
purein interfaceMonadRec<S,A>- Specified by:
purein interfaceMonadWriter<S,A,State<S,?>>- 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> State<S,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,A>- Specified by:
fmapin interfaceFunctor<S,A>- Specified by:
fmapin interfaceMonad<S,A>- Specified by:
fmapin interfaceMonadReader<S,A,State<S,?>>- Specified by:
fmapin interfaceMonadRec<S,A>- Specified by:
fmapin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
public <B> State<S,B> zip(Applicative<Fn1<? super A,? extends B>,State<S,?>> 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,A>- Specified by:
zipin interfaceMonad<S,A>- Specified by:
zipin interfaceMonadReader<S,A,State<S,?>>- Specified by:
zipin interfaceMonadRec<S,A>- Specified by:
zipin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
appFn- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public <B> Lazy<State<S,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,State<S,?>>> 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,A>- Specified by:
lazyZipin interfaceMonad<S,A>- Specified by:
lazyZipin interfaceMonadReader<S,A,State<S,?>>- Specified by:
lazyZipin interfaceMonadRec<S,A>- Specified by:
lazyZipin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
Maybe,Either
-
discardR
public <B> State<S,A> discardR(Applicative<B,State<S,?>> appB)
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<S,A>- Specified by:
discardRin interfaceMonad<S,A>- Specified by:
discardRin interfaceMonadReader<S,A,State<S,?>>- Specified by:
discardRin interfaceMonadRec<S,A>- Specified by:
discardRin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
discardL
public <B> State<S,B> discardL(Applicative<B,State<S,?>> appB)
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<S,A>- Specified by:
discardLin interfaceMonad<S,A>- Specified by:
discardLin interfaceMonadReader<S,A,State<S,?>>- Specified by:
discardLin interfaceMonadRec<S,A>- Specified by:
discardLin interfaceMonadWriter<S,A,State<S,?>>- Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
trampolineM
public <B> State<S,B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,State<S,?>>> 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,A>- 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
-
get
public static <A> State<A,A> get()
Create aStatethat simply returns back the initial state as both the result and the final state- Type Parameters:
A- the state and result type- Returns:
- the new
Stateinstance
-
put
public static <S> State<S,Unit> put(S s)
- Type Parameters:
S- the state type- Parameters:
s- the final state- Returns:
- the new
Stateinstance
-
gets
public static <S,A> State<S,A> gets(Fn1<? super S,? extends A> fn)
Create aStatethat maps its initial state into its result, but leaves the initial state unchanged.- Type Parameters:
S- the state typeA- the result type- Parameters:
fn- the mapping function- Returns:
- the new
Stateinstance
-
modify
public static <S> State<S,Unit> modify(Fn1<? super S,? extends S> fn)
- Type Parameters:
S- the state type- Parameters:
fn- the mapping function- Returns:
- the new
Stateinstance
-
state
public static <S,A> State<S,A> state(A a)
Create aStatethat returnsaas its result and its initial state as its final state.- Type Parameters:
S- the state typeA- the result type- Parameters:
a- the result- Returns:
- the new
Stateinstance
-
state
public static <S,A> State<S,A> state(Fn1<? super S,? extends Tuple2<A,S>> stateFn)
Create aStatefromstateFn, a function that maps an initial state into a result and a final state.- Type Parameters:
S- the state typeA- the result type- Parameters:
stateFn- the state function- Returns:
- the new
Stateinstance
-
-