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 s a. State s a -> s -> Tuple a s

Run a computation in the State monad

#evalState Source

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

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

#execState Source

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

Run a computation in the State monad, discarding the result

#mapState Source

mapState :: forall s a b. (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 s a. (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 MonadState :: Type -> (Type -> Type) -> Constraintclass (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 m s. MonadState s m => s -> m Unit

Set the state.

#modify_ Source

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

#modify Source

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

Modify the state by applying a function to the current state. The returned value is the new state value.

#gets Source

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

Get a value which depends on the current state.

#get Source

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

Get the current state.

Re-exports from Control.Monad.State.Trans

#StateT Source

newtype StateT :: Type -> (Type -> Type) -> Type -> Typenewtype 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 :: ((Type -> Type) -> Type -> Type) -> Constraintclass 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 s m a. (s -> s) -> StateT s m a -> StateT s m a

Modify the final state in a StateT monad action.

#runStateT Source

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

Run a computation in the StateT monad.

#mapStateT Source

mapStateT :: forall s m1 m2 a b. (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 s m a. Functor m => StateT s m a -> s -> m s

Run a computation in the StateT monad discarding the result.

#evalStateT Source

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

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