Get the input state, without modifying the state.
Inspect a value from the input state, without modifying the state.
Modify the context F
using transformation f
.
Modify the state (S
) component.
Run with the provided initial state value
Run with the provided initial state value and return the final value (discarding the final state).
Run with S
's empty monoid value as the initial state.
Run with S
's empty monoid value as the initial state and return the final
value (discarding the final state).
Run with S
's empty monoid value as the initial state and return the final
state (discarding the final value).
Run with the provided initial state value and return the final state (discarding the final value).
Like map, but also allows the state (S
) value to be modified.
Like transform, but allows the context to change from F
to G
.
Like transform, but allows the context to change from F
to G
.
scala> import cats.implicits._ scala> type ErrorOr[A] = Either[String, A] scala> val xError: IndexedStateT[ErrorOr, Int, Int, Int] = IndexedStateT.get scala> val xOpt: IndexedStateT[Option, Int, Int, Int] = xError.transformF(_.toOption) scala> val input = 5 scala> xError.run(input) res0: ErrorOr[(Int, Int)] = Right((5,5)) scala> xOpt.run(5) res1: Option[(Int, Int)] = Some((5,5))
Transform the state used.
Transform the state used.
This is useful when you are working with many focused StateT
s and want to pass in a
global state containing the various states needed for each individual StateT
.
scala> import cats.implicits._ // needed for StateT.apply scala> type GlobalEnv = (Int, String) scala> val x: StateT[Option, Int, Double] = StateT((x: Int) => Option((x + 1, x.toDouble))) scala> val xt: StateT[Option, GlobalEnv, Double] = x.transformS[GlobalEnv](_._1, (t, i) => (i, t._2)) scala> val input = 5 scala> x.run(input) res0: Option[(Int, Double)] = Some((6,5.0)) scala> xt.run((input, "hello")) res1: Option[(GlobalEnv, Double)] = Some(((6,hello),5.0))
IndexedStateT[F, SA, SB, A]
is a stateful computation in a contextF
yielding a value of typeA
. The state transitions from a value of typeSA
to a value of typeSB
.Note that for the
SA != SB
case, this is an indexed monad. Indexed monads are monadic type constructors annotated by an additional type for effect tracking purposes. In this case, the annotation tracks the initial state and the resulting state.Given
IndexedStateT[F, S, S, A]
, this yields theStateT[F, S, A]
monad.