Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal monad schedule class #376

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add AccumT example
Manuel Bärenz authored and turion committed Nov 29, 2024

Verified

This commit was signed with the committer’s verified signature.
mikekasprzak Mike Kasprzak
commit ac7faad6aaeafa555c162e7d51c1e6d5303432eb
8 changes: 8 additions & 0 deletions rhine-examples/rhine-examples.cabal
Original file line number Diff line number Diff line change
@@ -84,6 +84,14 @@ executable RandomWalk
random >=1.1,
simple-affine-space

executable Accum
import: opts
main-is: Accum.hs
build-depends:
transformers,
text


flag dev
description: Enable warnings as errors. Active on ci.
default: False
38 changes: 38 additions & 0 deletions rhine-examples/src/Accum.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- | 'AccumT' example

-- base
import Control.Monad (void)
import Control.Monad.IO.Class

-- transformers
import Control.Monad.Trans.Accum (AccumT, runAccumT, add, look)

-- text
import Data.Text (Text, pack)

-- rhine
import FRP.Rhine hiding (add)

personalMessage :: ClSF (AccumT [Text] IO) (Lifting StdinClock) () ()
personalMessage = tagS >>> arrMCl (pure >>> add)

type Lifting = LiftClock IO (AccumT [Text])
type EveryTwoSeconds = Lifting (Millisecond 2000)

everyTwoSeconds :: EveryTwoSeconds
everyTwoSeconds = liftClock waitClock

logSoFar :: ClSF (AccumT [Text] IO) EveryTwoSeconds () ()
logSoFar = sinceStart >>> arrMCl printLog
where
printLog t = do
add ["Time since start: " <> pack (show t)]
l <- look
liftIO $ do
putStrLn "Log so far:"
print l

main :: IO ()
main = do
putStrLn "You can add a personal message to the log."
void $ flip runAccumT [] $ flow $ personalMessage @@ liftClock StdinClock |@| logSoFar @@ everyTwoSeconds