1{-# LANGUAGE FlexibleContexts #-}
3-- Copyright : (c) 2017 Harendra Kumar
4-- (c) 2013, 2014 Gabriel Gonzalez
6-- This example is adapted from Gabriel Gonzalez's pipes-concurrency package.
7-- https://hackage.haskell.org/package/pipes-concurrency-2.0.8/docs/Pipes-Concurrent-Tutorial.html
10import Streamly.Prelude as S
11import Control.Monad (void)
12import Control.Monad.IO.Class (MonadIO(liftIO))
13import Control.Monad.State (MonadState, get, modify, runStateT)
15data Event = Quit | Harm Int | Heal Int deriving (Show)
17userAction :: MonadAsync m => SerialT m Event
18userAction = S.repeatM $ liftIO askUser
23 "potion" -> return (Heal 10)
24 "harm" -> return (Harm 10)
26 _ -> putStrLn "Type potion or harm or quit" >> askUser
28acidRain :: MonadAsync m => SerialT m Event
29acidRain = asyncly $ constRate 1 $ S.repeatM $ liftIO $ return $ Harm 1
31data Result = Check | Done
33runEvents :: (MonadAsync m, MonadState Int m) => SerialT m Result
35 event <- userAction `parallel` acidRain
37 Harm n -> modify (\h -> h - n) >> return Check
38 Heal n -> modify (\h -> h + n) >> return Check
41data Status = Alive | GameOver deriving Eq
43getStatus :: (MonadAsync m, MonadState Int m) => Result -> m Status
46 Done -> liftIO $ putStrLn "You quit!" >> return GameOver
50 then putStrLn "You die!" >> return GameOver
51 else putStrLn ("Health = " <> show h) >> return Alive
55 putStrLn "Your health is deteriorating due to acid rain,\
56 \ type \"potion\" or \"quit\""
57 let runGame = S.drainWhile (== Alive) $ S.mapM getStatus runEvents
58 void $ runStateT runGame 60
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list