packages feed

reactive-banana 0.8.0.0 → 0.8.0.1

raw patch · 5 files changed

+163/−7 lines, 5 filesdep ~transformers

Dependency ranges changed: transformers

Files

CHANGELOG.md view
@@ -1,6 +1,11 @@ Changelog for the `reactive-banana` package ------------------------------------------- +**version 0.8.0.1**++* New examples `Counter.hs` and `Octave.hs`.+* Bump `transfomers` dependency.+ **version 0.8.0.0**  * A new module `Reactive.Banana.Prim` exports primitive combinators that you can use to implement your own FRP library with a different API.
+ doc/examples/Counter.hs view
@@ -0,0 +1,77 @@+{-----------------------------------------------------------------------------+    reactive-banana+    +    Example: Actuate and pause an event network acting as a counter+------------------------------------------------------------------------------}+import Control.Monad (when)+import System.IO++import Reactive.Banana+import Reactive.Banana.Frameworks+++main :: IO ()+main = do+    displayHelpMessage+    sources <- (,,) <$> newAddHandler <*> newAddHandler <*> newAddHandler+    network <- setupNetwork sources+    actuate network+    eventLoop sources network++displayHelpMessage :: IO ()+displayHelpMessage = mapM_ putStrLn $+    "Commands are:":+    "   +   - increase counterUp event":+    "   -   - decrease counterUp event":+    "   p   - pause event network":+    "   a   - actuate event network":+    "   q   - quit the program":+    "":+    []++-- Read commands and fire corresponding events +eventLoop :: (EventSource (), EventSource (),EventSource EventNetwork) -> EventNetwork -> IO ()+eventLoop (eplus, eminus, espause) network = loop+    where+    loop = do+        putStr "> "+        hFlush stdout+        hSetBuffering stdin NoBuffering+        s <- getChar+        case s of+            '+'   -> fire eplus ()+            '-'   -> fire eminus ()+            'p'   -> fire espause network+            'a'   -> actuate network+            'q'   -> return ()+            _     -> putStrLn $ [s] ++ " - unknown command"+        when (s /= 'q') loop++{-----------------------------------------------------------------------------+    Event sources+------------------------------------------------------------------------------}+-- Event Sources - allows you to register event handlers+-- Your GUI framework should provide something like this for you+type EventSource a = (AddHandler a, a -> IO ())++addHandler :: EventSource a -> AddHandler a+addHandler = fst++fire :: EventSource a -> a -> IO ()+fire = snd++{-----------------------------------------------------------------------------+    Program logic+------------------------------------------------------------------------------}+-- Set up the program logic in terms of events and behaviors.+setupNetwork :: (EventSource (), EventSource (), EventSource EventNetwork) -> IO EventNetwork+setupNetwork (eplus, eminus, espause) = compile $ do+    counterUp   <- fromAddHandler (addHandler eplus)+    counterDown <- fromAddHandler (addHandler eminus)+    epause      <- fromAddHandler (addHandler espause)++    let ecount = accumE 0 $ ((+1) <$ counterUp) `union` (subtract 1 <$ counterDown)++    reactimate $ fmap print ecount+    reactimate $ fmap pause epause+
+ doc/examples/Octave.hs view
@@ -0,0 +1,79 @@+{-----------------------------------------------------------------------------+    reactive-banana+    +    Example: "The world's worst synthesizer"+    from the unofficial tutorial.+    <http://www.haskell.org/haskellwiki/FRP_explanation_using_reactive-banana>+------------------------------------------------------------------------------}+module Main where++import Data.Char (toUpper)+import Control.Monad (forever)+import System.IO (BufferMode(..), hSetEcho, hSetBuffering, stdin)+import Reactive.Banana+import Reactive.Banana.Prim (addHandler)+import Reactive.Banana.Frameworks+++type Octave = Int++data Pitch = PA | PB | PC | PD | PE | PF | PG+    deriving (Eq, Enum)++-- Mapping between pitch and the char responsible for it.+pitchChars :: [(Pitch, Char)]+pitchChars = [(p, toEnum $ fromEnum 'a' + fromEnum p) |+              p <- [PA .. PG]]++-- Reverse of pitchChars+charPitches :: [(Char, Pitch)]+charPitches = [(b, a) | (a, b) <- pitchChars]++data Note = Note Octave Pitch++instance Show Pitch where+    show p = case lookup p pitchChars of+        Nothing -> error "cannot happen"+        Just c  -> [toUpper c]++instance Show Note where+    show (Note o p) = show p ++ show o++-- Filter and transform events at the same time.+filterMapJust :: (a -> Maybe b) -> Event t a -> Event t b+filterMapJust f = filterJust . fmap f++-- Change the original octave by adding a number of octaves, taking+-- care to limit the resulting octave to the 0..10 range.+changeOctave :: Int -> Octave -> Octave+changeOctave d = max 0 . min 10 . (d+)++-- Get the octave change for the '+' and '-' chars.+getOctaveChange :: Char -> Maybe Int+getOctaveChange c = case c of+    '+' -> Just 1+    '-' -> Just (-1)+    _ -> Nothing++makeNetworkDescription :: Frameworks t => AddHandler Char -> Moment t ()+makeNetworkDescription addKeyEvent = do+    eKey <- fromAddHandler addKeyEvent+    let+        eOctaveChange = filterMapJust getOctaveChange eKey+        bOctave = accumB 3 (changeOctave <$> eOctaveChange)+        ePitch = filterMapJust (`lookup` charPitches) eKey+        bPitch = stepper PC ePitch+        bNote = Note <$> bOctave <*> bPitch+        foo = Note 0 PA+    eNoteChanged <- changes bNote+    reactimate' $ fmap (\n -> putStrLn ("Now playing " ++ show n))+                 <$> eNoteChanged++main :: IO ()+main = do+    (addKeyEvent, fireKey) <- newAddHandler+    network <- compile (makeNetworkDescription addKeyEvent)+    actuate network+    hSetEcho stdin False+    hSetBuffering stdin NoBuffering+    forever (getChar >>= fireKey)
doc/examples/SlotMachine.hs view
@@ -78,11 +78,6 @@ -- A win consist of either double or triple numbers data Win = Double | Triple --- payout for each win-payout :: Win -> Money-payout Double = 20-payout Triple = 200-  -- Set up the program logic in terms of events and behaviors. setupNetwork :: forall t. Frameworks t => 
reactive-banana.cabal view
@@ -1,5 +1,5 @@ Name:                reactive-banana-Version:             0.8.0.0+Version:             0.8.0.1 Synopsis:            Library for functional reactive programming (FRP). Description:     Reactive-banana is a library for Functional Reactive Programming (FRP).@@ -59,7 +59,7 @@          build-depends:      base >= 4.2 && < 5,                         containers >= 0.3 && < 0.6,-                        transformers >= 0.2 && < 0.4,+                        transformers >= 0.2 && < 0.5,                         vault == 0.3.*      extensions:         EmptyDataDecls,