Last active
May 26, 2024 21:48
-
-
Save Warwolt/d83db4dada02193e657918ad102f547d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
import Control.Monad.Reader | |
-- FRP | |
type Time = Float | |
type Behavior a = (Time -> a) | |
type Event a = (Time, a) | |
inf :: Time | |
inf = 1/0 | |
untilB :: Behavior a -> Event (Behavior a) -> Behavior a | |
b `untilB` (t', b') = \t -> if t <= t' then b t else b' t | |
infixr 1 `untilB` | |
(==>) :: Event a -> (Time -> a -> b) -> Event b | |
(t,x) ==> f = (t, f t x) | |
infixl 3 ==> | |
-- Platform | |
type ButtonPressE = Event () | |
-- App | |
data Color = Red | Green deriving Show | |
wait :: Time -> Event () | |
wait t = (t, ()) | |
red :: Behavior Color | |
red t = Red | |
green :: Behavior Color | |
green t = Green | |
cycleRedGreen :: Time -> Behavior Color | |
cycleRedGreen = | |
\t0 -> red `untilB` wait (t0 + 1) ==> | |
\t1 _ -> green `untilB` wait (t1 + 1) ==> | |
\t2 _ -> cycleRedGreen t2 | |
buttonPress :: Time -> ButtonPressE | |
buttonPress t = (inf, ()) | |
buttonCycleRedGreen :: Time -> Behavior Color | |
buttonCycleRedGreen = | |
\t0 -> red `untilB` buttonPress t0 ==> | |
\t1 _ -> green | |
main :: IO () | |
main = do | |
forM_ [1..10] (\t -> (putStrLn (show (buttonCycleRedGreen 0 t)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Animating sinusoids with delayed animation start
Trying out some non-constant behaviors by using
sin
andcos
. The animation has a delayed start, and then gives one cycle ofsin
followed by one cycle ofcos
.The delayed start is done with a "time transformation".
The general time transformation is given with
shiftB :: Behavior Time -> Behavior a -> Behavior a
which basically just applies a function ont
before passing it to the behavior. This is used indelayB :: Time -> Behavior a -> Behavior a
which just slides the behavior to the right along the time axis.