Last active
September 4, 2015 10:07
-
-
Save geoffrasb/0952e7fc2920addcd061 to your computer and use it in GitHub Desktop.
FRP learning note
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
reference: Functional Reactive Animation - by Conal Elliott et al | |
* 2 things: | |
- Behavior a | |
- Event a | |
* semantic functions | |
- at :: Behavior a -> Time -> a | |
- occ :: Event a -> (Time, a) | |
* functions related to Behavior | |
- time :: Behavior Time | |
-> at time t = t | |
- lift2 :: (a1 -> a2 -> b) -> | |
Behavior a1 -> Behavior a2 -> Behavior b | |
- untilB :: Behavior a -> Event (Behavior a) -> Behavior a | |
-> at (b untilB e) t = | |
if t<=te then (at b t) else (at b' t) | |
where (te,b') = occ e | |
* Behavior example | |
- wiggle :: Behavior R | |
-> wiggle = sin (pi * time) | |
- wiggleRange lo hi = lo + (hi-lo) * (wiggle+1)/2 | |
* functions related to Event | |
- (+=>) :: Event a -> (Time -> a -> b) -> Event b | |
-> occ (e +=> f) = (te, f te x) | |
where (te,x) = occ e | |
- (==>) :: Event a -> (a -> b) -> Event b | |
- (*=>) :: Event a -> (Time -> b) -> Event b | |
- (-=>) :: Event a -> b -> Event b | |
- click :: Time -> Event (Event ()) | |
example: b1 `untilB` (click t0) ==> \e -> | |
b2 `untilB` e -=> | |
b3 | |
- predicate :: Behavior Bool -> Time -> Event () | |
-> occ (predicate b t0) = (inf {t > t0 | (at b t)}, ()) | |
example: b1 `untilB` (predicate (sin time = 0.5) t0) | |
-=> b2 | |
- (.|.) :: Event a -> Event a -> Event a --choice | |
example: 一個預設值為0, 當按下左鍵會變成-1, 按下右鍵會變成1 | |
bSign :: Time -> Behavior Int | |
bSign t0 = | |
0 `untilB` leftClick t0 ==> nonZero (-1) .|. | |
rightClick t0 ==> nonZero 1 | |
where nonZero r stop = | |
r `untilB` stop *=> bSign | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
另一個寫法(但是不正確)