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 thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
.<B> State<S,A>
discardR(Applicative<B,State<S,?>> appB)
Sequence both thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
.A
eval(S s)
Run the stateful computation, returning the result.S
exec(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 aState
that 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 aState
that 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 alazy
instance 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's
environment 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 valueb
into 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 aTuple2
of the result and the final state.static <S,A>
State<S,A>state(A a)
Create aState
that returnsa
as 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 aState
fromstateFn
, 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 aRecursiveResult
inside thisMonadRec
, internally trampoline the operation until it yields atermination
instruction.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 aTuple2
of the result and the final state.- Parameters:
s
- the initial state- Returns:
- a
Tuple2
of 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's
environment after reading it but before running the effect.- Specified by:
local
in interfaceMonadReader<S,A,State<S,?>>
- Parameters:
fn
- the modification function- Returns:
- the
MonadReader
with 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:
listens
in 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:
censor
in 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:
flatMap
in interfaceMonad<S,A>
- Specified by:
flatMap
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
flatMap
in interfaceMonadRec<S,A>
- Specified by:
flatMap
in 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 valueb
into this applicative functor.- Specified by:
pure
in interfaceApplicative<S,A>
- Specified by:
pure
in interfaceMonad<S,A>
- Specified by:
pure
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
pure
in interfaceMonadRec<S,A>
- Specified by:
pure
in 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:
fmap
in interfaceApplicative<S,A>
- Specified by:
fmap
in interfaceFunctor<S,A>
- Specified by:
fmap
in interfaceMonad<S,A>
- Specified by:
fmap
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
fmap
in interfaceMonadRec<S,A>
- Specified by:
fmap
in 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:
zip
in interfaceApplicative<S,A>
- Specified by:
zip
in interfaceMonad<S,A>
- Specified by:
zip
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
zip
in interfaceMonadRec<S,A>
- Specified by:
zip
in 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 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<S,A>
- Specified by:
lazyZip
in interfaceMonad<S,A>
- Specified by:
lazyZip
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
lazyZip
in interfaceMonadRec<S,A>
- Specified by:
lazyZip
in 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 thisApplicative
andappB
, discardingappB's
result and returning thisApplicative
. This is generally useful for sequentially performing side-effects.- Specified by:
discardR
in interfaceApplicative<S,A>
- Specified by:
discardR
in interfaceMonad<S,A>
- Specified by:
discardR
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
discardR
in interfaceMonadRec<S,A>
- Specified by:
discardR
in 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 thisApplicative
andappB
, discarding thisApplicative's
result and returningappB
. This is generally useful for sequentially performing side-effects.- Specified by:
discardL
in interfaceApplicative<S,A>
- Specified by:
discardL
in interfaceMonad<S,A>
- Specified by:
discardL
in interfaceMonadReader<S,A,State<S,?>>
- Specified by:
discardL
in interfaceMonadRec<S,A>
- Specified by:
discardL
in 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 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<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 aState
that 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
State
instance
-
put
public static <S> State<S,Unit> put(S s)
- Type Parameters:
S
- the state type- Parameters:
s
- the final state- Returns:
- the new
State
instance
-
gets
public static <S,A> State<S,A> gets(Fn1<? super S,? extends A> fn)
Create aState
that 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
State
instance
-
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
State
instance
-
state
public static <S,A> State<S,A> state(A a)
Create aState
that returnsa
as 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
State
instance
-
state
public static <S,A> State<S,A> state(Fn1<? super S,? extends Tuple2<A,S>> stateFn)
Create aState
fromstateFn
, 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
State
instance
-
-