Skip to content

Instantly share code, notes, and snippets.

@teh
Created October 15, 2015 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teh/7da27e48b74ec658dd4d to your computer and use it in GitHub Desktop.
Save teh/7da27e48b74ec658dd4d to your computer and use it in GitHub Desktop.
import Reactive.Banana (compile)
import Reactive.Banana.Frameworks (newAddHandler, fromAddHandler, actuate, reactimate)
import Control.Concurrent (threadDelay, forkIO)
import Control.Monad (forever)
import Data.Char (toUpper)
main = do
-- newAddHandler returns an AddHandler (roughly a callback) and a
-- way to fire the callback (fire).
(es, fire) <- newAddHandler
-- We fire the handler with the value "hello" every 100 ms.
forkIO $ forever $ do
threadDelay (1000 * 100)
fire "hello"
-- Create a simple reactive network that prints the value passed
-- to the callback, once as passed, then as uppercase.
network <- compile $ do
c <- fromAddHandler es
reactimate $ fmap print c
reactimate $ fmap print (fmap (map toUpper) c)
-- actuate "activates" the network (registers callbacks etc.), can
-- be paused with `pause`
actuate network
getLine >> return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment