Created
April 3, 2012 14:13
-
-
Save bluescreen303/2292308 to your computer and use it in GitHub Desktop.
wrapping reactive banana
This file contains hidden or 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
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