Skip to content

Instantly share code, notes, and snippets.

@bluescreen303
Created April 3, 2012 14:13
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 bluescreen303/2292308 to your computer and use it in GitHub Desktop.
Save bluescreen303/2292308 to your computer and use it in GitHub Desktop.
wrapping reactive banana
interpretModel :: (forall t. Event t a -> Event t b) -> [[a]] -> [[b]]
interpretModel f xs = map toList $ (unE . f . E) (map Just xs)
where toList :: Maybe [a] -> [a]
toList Nothing = []
toList (Just xs) = xs
runner :: (forall t. Event t a -> Event t b) -> IO ([a] -> IO [[b]])
runner network = do inbox <- newIORef [] -- ref to write events into
outbox <- newIORef . interpretModel network =<< toEvents inbox
return $ step inbox outbox
toEvents :: IORef [a] -> IO [[a]]
toEvents ref = unsafeInterleaveIO $ do
current <- readIORef ref
writeIORef ref []
future <- toEvents ref
return $ current : future
step :: IORef [a] -> IORef [[b]] -> [a] -> IO [[b]]
step inbox outbox inEvent = do writeIORef inbox inEvent
outs <- readIORef outbox
let (_, outs') = span null outs
(current, future) = break null outs'
writeIORef outbox future
return current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment