Skip to content

Instantly share code, notes, and snippets.

@greyson
Created July 10, 2015 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyson/5c232bce3177f855fe50 to your computer and use it in GitHub Desktop.
Save greyson/5c232bce3177f855fe50 to your computer and use it in GitHub Desktop.
Event streams of stateful operations with shared accumulated state.
{-# LANGUAGE RecursiveDo #-}
module FRP.Sodium.State
( runAccum
, snapshotState
) where
import Control.Monad.State
import FRP.Sodium
type AccumE s = State (Behaviour s, Event s)
-- | Build accumulated state operation streams with an initial state.
runAccum :: s -> AccumE s a -> Reactive a
runAccum initValue op = do
rec
let (result, (_, asChanges)) = runState op (behaviour, never)
behaviour <- hold initValue asChanges
return result
-- | Snapshot stateful events with the accumulated state.
snapshotState :: Event (State s a) -> AccumE s (Event a)
snapshotState op = do
st <- fst <$> get
let (result, changes) = splitE $ snapshot (\f b -> runState f b) op st
modify $ \(b, stream) -> (b, stream `merge` changes)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment