bluefin-0.0.0.0
Safe HaskellNone
LanguageHaskell2010

Bluefin.State

Synopsis

Handle

data State s (st :: Effects) Source #

A handle to a strict mutable state of type a

Handlers

evalState Source #

Arguments

:: forall s (es :: Effects) a. s

Initial state

-> (forall (st :: Effects). State s st -> Eff (st :& es) a)

Stateful computation

-> Eff es a

Result

>>> runPureEff $ evalState 10 $ \st -> do
      n <- get st
      pure (2 * n)
20

runState Source #

Arguments

:: forall s (es :: Effects) a. s

Initial state

-> (forall (st :: Effects). State s st -> Eff (st :& es) a)

Stateful computation

-> Eff es (a, s)

Result and final state

>>> runPureEff $ runState 10 $ \st -> do
      n <- get st
      pure (2 * n)
(20,10)

Effectful operations

get Source #

Arguments

:: forall (st :: Effects) (es :: Effects) s. st :> es 
=> State s st 
-> Eff es s

The current value of the state

>>> runPureEff $ runState 10 $ \st -> do
      n <- get st
      pure (2 * n)
(20,10)

put Source #

Arguments

:: forall (st :: Effects) (es :: Effects) s. st :> es 
=> State s st 
-> s

The new value of the state. The new value is forced before writing it to the state.

-> Eff es () 

Set the value of the state

>>> runPureEff $ runState 10 $ \st -> do
      put st 30
((), 30)

modify Source #

Arguments

:: forall (st :: Effects) (es :: Effects) s. st :> es 
=> State s st 
-> (s -> s)

Apply this function to the state. The new value of the state is forced before writing it to the state.

-> Eff es () 
>>> runPureEff $ runState 10 $ \st -> do
      modify st (* 2)
((), 20)