]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - src/RMCA/Auxiliary/Yampa.hs
Changed maybeIf/eventIf with guard.
[tmp/julm/arpeggigon.git] / src / RMCA / Auxiliary / Yampa.hs
1 {-# LANGUAGE Arrows #-}
2
3 module RMCA.Auxiliary.Yampa where
4
5 import FRP.Yampa
6 import Data.Maybe
7 import RMCA.Auxiliary.Misc
8 import Control.Monad
9
10 -- | = Yampa
11
12 countTo :: (Integral b) => b -> SF (Event a) (Event b)
13 countTo n = count >>^ filterE (== n)
14
15 -- | Synchonizes two event sources. An event on the first source will be delayed until an event occurs on the second.
16 --
17 -- Ex:
18 -- Event a => . . 1 . . . . 2 . . . 3 . . 4 . . . . . 5 . . 6 . . . . .
19 -- Event b => . a . . . b . . . c . . . . . . d . e . f . . . . . g . .
20 -- wairFor => . . . . . 1 . . . 2 . . . . . . 4 . . . 5 . . . . . 6 . .
21
22 waitForEvent :: SF (Event a, Event b) (Event a)
23 waitForEvent = proc (ea,eb) -> do
24 em <- arr $ uncurry $ mapMerge Left Right (\_ b -> Right b) -< (ea,eb)
25 hob <- dAccumHoldBy accumulator NoEvent -< em
26 returnA -< eb *> (ea `lMerge` hob)
27 where accumulator :: Event a -> Either a b -> Event a
28 accumulator _ (Left a) = Event a
29 accumulator _ (Right _) = NoEvent
30 --accumulator _ (Right b) =
31
32 {-
33 waitForEvent :: SF (Event b, Event a) (Event b)
34 waitForEvent = proc (eb,ea) -> do
35 rec
36 es' <- iPre NoEvent -< es
37 es <- rSwitch waitAux -< ((eb,ea),es' `tag` waitAux)
38 returnA -< es
39 where waitAux = proc (eb,ea) -> do
40 --ea' <- (if b then notYet else identity) -< ea
41 eb' <- accumHoldBy (\_ b -> Event b) NoEvent -< eb
42 returnA -< ea *> eb'
43 -}
44 -- | 'stepBack' contains its previous argument as its output. Because it's hard to define it at time 0, it's wrapped up in a 'Maybe'.
45 stepBack :: SF a (Maybe a)
46 stepBack = sscan f (Nothing, Nothing) >>^ snd
47 where f :: (Maybe a, Maybe a) -> a -> (Maybe a, Maybe a)
48 f (Nothing,_) x' = (Just x', Nothing)
49 f (Just x, _) x' = (Just x', Just x)
50
51 -- | Like 'stepBack' but the output value is always defined and is equal to the input at time 0.
52 stepBack' :: SF a a
53 stepBack' = proc x -> do
54 x' <- stepBack -< x
55 returnA -< fromMaybe x x'
56
57 -- | Throws an 'Event' when the incoming signal change. The 'Event' is tagged with the new value.
58 onChange :: (Eq a) => SF a (Event a)
59 onChange = proc x -> do
60 x' <- stepBack -< x
61 let makeEvent x x'
62 | isNothing x' = NoEvent
63 | otherwise = let x'' = fromJust x' in
64 if x'' == x then NoEvent else Event x
65 returnA -< makeEvent x x'
66
67 -- | Similar to 'onChange' but contains its initial value in the first
68 -- event.
69 onChange' :: (Eq a) => SF a (Event a)
70 onChange' = proc x -> do
71 x' <- stepBack -< x
72 -- If it's the first value, throw an Event, else behave like onChange.
73 let makeEvent x x'
74 | isNothing x' = Event x
75 | otherwise = let x'' = fromJust x' in
76 if x'' == x then NoEvent else Event x
77 returnA -< makeEvent x x'
78
79 -- | Integrates some variable modulo something.
80 integralMod :: (Real a, VectorSpace a s) => a -> SF a a
81 integralMod x = intMod' 0
82 where intMod' x0 = switch (intMod'' x0) (\y -> intMod' (y - x))
83 intMod'' x0 = proc t -> do
84 it <- (+ x0) ^<< integral -< t
85 es <- edgeBy (\_ y -> guard (y > x) $> y) 0 -< it
86 returnA -< (it,es)
87
88
89
90 -- | Generates a sine function whose period is given as a varying input.
91 varFreqSine :: SF DTime Double
92 varFreqSine = sin ^<< (2*pi*) ^<< integralMod 1 <<^ (1/)
93
94 -- | Generates an 'Event' with a regular period, which is given as an input to the signal function.
95 repeatedlyS :: a -> SF DTime (Event a)
96 repeatedlyS x = edgeBy (\a b -> guard (a * b < 0) $> x) 0
97 <<< varFreqSine <<^ (2*)
98
99 repeatedlyS' :: a -> SF DTime (Event a)
100 repeatedlyS' x = (repeatedlyS x &&& now x) >>> arr (uncurry lMerge)
101
102 -- |
103 -- = Curry and uncurry functions