diff --git a/automaton/automaton.cabal b/automaton/automaton.cabal index 994b691b..a35eb46c 100644 --- a/automaton/automaton.cabal +++ b/automaton/automaton.cabal @@ -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 diff --git a/automaton/examples/ForeverSawtooth.hs b/automaton/examples/ForeverSawtooth.hs new file mode 100644 index 00000000..c16c6945 --- /dev/null +++ b/automaton/examples/ForeverSawtooth.hs @@ -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 diff --git a/automaton/examples/UserSawtooth.hs b/automaton/examples/UserSawtooth.hs new file mode 100644 index 00000000..f17da8fd --- /dev/null +++ b/automaton/examples/UserSawtooth.hs @@ -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