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

Bluefin.StateSource

Synopsis

Documentation

A StateSource allows you to allocate new State handles, much like ST allows you to allocate new STRefs. This can be useful when you want to avoid nested runState (or evalState) blocks, or you need a number of mutable states that is only dynamically known.

Handle

data StateSource (st :: Effects) #

Handle to a capability to create strict mutable state handles

Handlers

withStateSource #

Arguments

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

͘

runPureEff $ withStateSource $ \source -> do
  n <- newState source 5
  total <- newState source 0

  withJump $ \done -> forever $ do
    n' <- get n
    modify total (+ n')
    when (n' == 0) $ jumpTo done
    modify n (subtract 1)

  get total
15

Effectful operations

newState #

Arguments

:: forall (e :: Effects) s (es :: Effects). StateSource e 
-> s

The initial value for the state handle

-> Eff es (State s e)

A new state handle

runPureEff $ withStateSource $ \source -> do
  n <- newState source 5
  total <- newState source 0

  withJump $ \done -> forever $ do
    n' <- get n
    modify total (+ n')
    when (n' == 0) $ jumpTo done
    modify n (subtract 1)

  get total
15