Module

Control.Monad.State

Package
purescript-transformers
Repository
purescript/purescript-transformers

This module defines the State monad.

#State Source

type State s = StateT s Identity

The State monad is a synonym for the StateT monad transformer, applied to the Identity monad.

#runState Source

runState :: forall a s. State s a -> s -> Tuple a s

Run a computation in the State monad

#evalState Source

evalState :: forall a s. State s a -> s -> a

Run a computation in the State monad, discarding the final state

#execState Source

execState :: forall a s. State s a -> s -> s

Run a computation in the State monad, discarding the result

#mapState Source

mapState :: forall b a s. (Tuple a s -> Tuple b s) -> State s a -> State s b

Change the type of the result in a State action

#withState Source

withState :: forall a s. (s -> s) -> State s a -> State s a

Modify the state in a State action

Re-exports from Control.Monad.State.Class

#MonadState Source

class (Monad m) <= MonadState s m | m -> s where

The MonadState s type class represents those monads which support a single piece of mutable state of type s.

  • state f updates the state using the function f.

An implementation is provided for StateT, and for other monad transformers defined in this library.

Laws:

  • do { get ; get } = get
  • do { put x ; put y } = put y
  • do { put x ; get } = put x $> x
  • do { s <- get ; put s } = pure unit

Members

#put Source

put :: forall s m. MonadState s m => s -> m Unit

Set the state.

#modify Source

modify :: forall m s. MonadState s m => (s -> s) -> m Unit

Modify the state by applying a function to the current state.

#gets Source

gets :: forall a m s. MonadState s m => (s -> a) -> m a

Get a value which depends on the current state.

#get Source

get :: forall s m. MonadState s m => m s

Get the current state.

Re-exports from Control.Monad.State.Trans

#StateT Source

newtype StateT s m a

The state monad transformer.

This monad transformer extends the base monad with the operations get and put which can be used to model a single piece of mutable state.

The MonadState type class describes the operations supported by this monad.

Constructors

Instances

#MonadTrans Source

class MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#withStateT Source

withStateT :: forall a m s. (s -> s) -> StateT s m a -> StateT s m a

Modify the final state in a StateT monad action.

#runStateT Source

runStateT :: forall a m s. StateT s m a -> s -> m (Tuple a s)

Run a computation in the StateT monad.

#mapStateT Source

mapStateT :: forall b a m2 m1 s. (m1 (Tuple a s) -> m2 (Tuple b s)) -> StateT s m1 a -> StateT s m2 b

Change the result type in a StateT monad action.

#execStateT Source

execStateT :: forall a m s. Functor m => StateT s m a -> s -> m s

Run a computation in the StateT monad discarding the result.

#evalStateT Source

evalStateT :: forall a m s. Functor m => StateT s m a -> s -> m a

Run a computation in the StateT monad, discarding the final state.