Created
July 10, 2015 00:15
-
-
Save greyson/5c232bce3177f855fe50 to your computer and use it in GitHub Desktop.
Event streams of stateful operations with shared accumulated state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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