Skip to content

Instantly share code, notes, and snippets.

@bradparker
Created November 26, 2018 10:32
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 bradparker/160a4b8932cd8e5c1d12aeadd92d88e7 to your computer and use it in GitHub Desktop.
Save bradparker/160a4b8932cd8e5c1d12aeadd92d88e7 to your computer and use it in GitHub Desktop.
Ok, let's have a crack at learning FRP
-- To run:
-- $ nix-shell -p "haskellPackages.ghcWithPackages (ghc: [ghc.reactive-banana])" --run "runhaskell ./Main.hs"
module Main where
import Control.Concurrent (threadDelay)
import Control.Event.Handler (newAddHandler)
import Control.Monad (replicateM_)
import Reactive.Banana.Combinators
( Event
, MonadMoment
, accumE
, filterE
, unionWith
)
import Reactive.Banana.Frameworks (actuate, compile, fromAddHandler, reactimate)
fizzBuzz :: MonadMoment m => Event () -> m (Event String)
fizzBuzz tick = do
count <- accumE 0 ((+ 1) <$ tick)
let fizz = "Fizz" <$ filterE ((0 ==) . (`mod` 3)) count
buzz = "Buzz" <$ filterE ((0 ==) . (`mod` 5)) count
both = unionWith (++) fizz buzz
pure $ unionWith const both (show <$> count)
main :: IO ()
main = do
(tickAddHandler, fireTick) <- newAddHandler
network <-
compile $ do
tick <- fromAddHandler tickAddHandler
eFizzBuzz <- fizzBuzz tick
reactimate $ putStrLn <$> eFizzBuzz
actuate network
replicateM_ 100 $ do
fireTick ()
threadDelay 100000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment