# Pastebin 4vKg7tBe {-# OPTIONS_GHC -Wall #-} import Control.Monad (forever) import Data.Foldable (traverse_) import Data.Functor ((<&>)) import Reactive.Banana import Reactive.Banana.Frameworks main :: IO () main = do -- Your SDL binding will be doing something like this, so you might -- not need it yourself. I just want a simple external event -- example. (lineAddHandler, onLine) <- newAddHandler -- Build and kick off the network compile (network lineAddHandler) >>= actuate -- Feed input events from main thread, because actuate runs in the -- background forever $ getLine >>= onLine network :: AddHandler String -> MomentIO () network lineAddHandler = do -- Make an Event String which fires on each line that comes in lineE <- fromAddHandler lineAddHandler -- Accumulate into a (Event, Behavior). let dup x = (x, x) (recentLinesE, recentLinesB) <- mapAccum [] $ lineE <&> \line acc -> dup . take 5 $ (line : acc) -- Print some messages reactimate $ recentLinesE <&> \recentLines -> do putStrLn "Recent lines (from Event):" traverse_ print recentLines -- Print some more messages reactimate $ -- Note that because frame updates are _atomic_ the Behavior we -- sample when lineE fires holds the value from the _current_ -- frame i.e. the new line is not present. recentLinesB <@ lineE <&> \recentLines -> do putStrLn "Recent lines (from Behavior):" traverse_ print recentLines