Skip to content

Commit

Permalink
Add documentation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Bärenz authored and turion committed Oct 24, 2024
1 parent 88bf91a commit 307fe7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions automaton/automaton.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ test-suite automaton-test
tasty-hunit ^>=0.10,
tasty-quickcheck >=0.10 && <0.12,

executable UserSawtooth
import: opts
hs-source-dirs: examples
main-is: UserSawtooth.hs
build-depends:
automaton

executable ForeverSawtooth
import: opts
hs-source-dirs: examples
main-is: ForeverSawtooth.hs
build-depends:
automaton

flag dev
description: Enable warnings as errors. Active on ci.
default: False
Expand Down
13 changes: 13 additions & 0 deletions automaton/examples/ForeverSawtooth.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- base
import Control.Arrow ((>>>))
import Control.Monad (guard)

-- automaton
import Data.Automaton
import Data.Automaton.Trans.Except

sawtooth :: Automaton IO a Int
sawtooth = forever $ try $ count >>> throwOnMaybe (\n -> guard (n > 10))

main :: IO ()
main = reactimate $ sawtooth >>> arrM print
18 changes: 18 additions & 0 deletions automaton/examples/UserSawtooth.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- base
import Control.Arrow ((>>>))
import Control.Monad (guard)

-- automaton
import Data.Automaton
import Data.Automaton.Trans.Except

userSawtooth :: Int -> Automaton IO a Int
userSawtooth nMax = safely $ do
try $ count >>> throwOnMaybe (\n -> guard (n > nMax))
nMax' <- once_ $ do
putStrLn "Maximum reached, please enter next nMax:"
readLn
safe $ userSawtooth nMax'

main :: IO ()
main = reactimate $ userSawtooth 10 >>> arrM print

0 comments on commit 307fe7d

Please sign in to comment.