Created
January 11, 2012 14:31
-
-
Save raimohanska/1594917 to your computer and use it in GitHub Desktop.
reactive-banana Events, Behaviors and Zipping with +
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
import Reactive.Banana | |
import Control.Concurrent | |
main = do | |
-- Create handles for pushing values into events | |
(addHandler1, push1) <- newAddHandler | |
(addHandler2, push2) <- newAddHandler | |
-- Create event network | |
network <- compile $ do | |
-- Actual events from AddHandlers | |
e1 <- fromAddHandler addHandler1 | |
e2 <- fromAddHandler addHandler2 | |
-- Behaviors from Events | |
let d1 = stepperD 0 e1 :: Discrete Int | |
let d2 = stepperD 0 e2 :: Discrete Int | |
-- Sum Behavior | |
let sumD = (+) <$> d1 <*> d2 | |
-- Back to Event | |
let sumE = changes sumD | |
-- Assign side-effects | |
reactimate $ fmap (prefixPrint "e1=") e1 | |
reactimate $ fmap (prefixPrint "e2=") e2 | |
reactimate $ fmap (prefixPrint "sum=") sumE | |
-- Fire it up | |
actuate network | |
-- Push data | |
push1 10 | |
push2 20 | |
-- Helper for printing values | |
prefixPrint p thing = putStrLn $ p ++ (show thing) |
Finally fixed it! Thanks to John L! http://stackoverflow.com/questions/8826970/whats-wrong-with-my-sum-event-in-reactive-banana
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not really work: the sumE event always gets an out-of-date value. Can you fix it?