withState -package:massiv

withState f m executes action m on a state modified by applying f.
>>> runPureEff $ withState 10 $ \st -> do
n <- get st
pure (s -> (2 * n, s))
(20,10)
>>> runPureEff $ withState 10 $ \st -> do
n <- get st
pure (s -> (2 * n, s))
(20,10)
withStateT f m executes action m on a state modified by applying f.
withStateT f m executes action m on a state modified by applying f.
Opens a prepared statement, executes an action using this statement, and closes the statement, even in the presence of exceptions.
Bracket prepare and finalize. Useful for stepping multiple times through a Statement
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
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
Set a flag in the parser to True before running a parser, then set the flag's value to False.
Evaluate the inner monad of a stream using the supplied stateful runner function and the initial state. The state returned by an invocation of the runner is supplied as input state to the next invocation.
Run a stateful parser with some initial state on a text. See also: runTextParser, runJournalParser.