bluefin-0.6.0.0: The Bluefin effect system
Safe HaskellNone
LanguageHaskell2010

Bluefin.Capability.Modify

Synopsis

Capability

type Modify = State #

Capability to modify a reference to an s

Handlers

evalModify #

Arguments

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

Initial value of modifyable state

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

Stateful computation

-> Eff es a

Result

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

runModify #

Arguments

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

Initial value of modifyable state

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

Stateful computation

-> Eff es (a, s)

Result and final state

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

withModify #

Arguments

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

Initial value of modifyable state

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

Stateful computation

-> Eff es a

Result

>>> runPureEff $ withModify 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)