bluefin-0.0.14.0: The Bluefin effect system
Safe HaskellSafe-Inferred
LanguageHaskell2010

Bluefin.State

Synopsis

Handle

data State s (e :: Effects) #

A handle to a strict mutable state of type s

Instances

Instances details
Handle (State s) 
Instance details

Defined in Bluefin.Internal

Methods

mapHandle :: forall (e :: Effects) (es :: Effects). e :> es => State s e -> State s es #

Handlers

evalState #

Arguments

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

Initial state

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

Stateful computation

-> Eff es a

Result

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

runState #

Arguments

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

Initial state

-> (forall (e :: Effects). State s e -> Eff (e :& 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)

withState #

Arguments

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

Initial state

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

Stateful computation

-> Eff es a

Result

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

Effectful operations

get #

Arguments

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

The current value of the state

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

put #

Arguments

:: forall (e :: Effects) (es :: Effects) s. e :> es 
=> State s e 
-> 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 #

Arguments

:: forall (e :: Effects) (es :: Effects) s. e :> es 
=> State s e 
-> (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)