on master help sign in
Make Automaton a bit nicer.
e3ac170evancz
core-elm/Automaton.elm+37−15
@@ -9,41 +9,63 @@ data Automaton a b = Automaton (a -> (b, Automaton a b))
9run :: Automaton a b -> Signal a -> Signal b
9run :: Automaton a b -> Signal a -> Signal b
10run :: OrderedContainer c => Automaton a b -> c a -> c b
10run :: OrderedContainer c => Automaton a b -> c a -> c b
11
11
12−compose :: Automaton a b -> Automaton b c -> Automaton a c
12+(>>>) :: Automaton a b -> Automaton b c -> Automaton a c
13+(<<<) :: Automaton b c -> Automaton a b -> Automaton a c
13
14
14combine :: [Automaton a b] -> Automaton a [b]
15combine :: [Automaton a b] -> Automaton a [b]
15combine :: Container c => c (Automaton a b) -> Automaton a (c b)
16combine :: Container c => c (Automaton a b) -> Automaton a (c b)
16
17
17
18
18−pure :: (a -> b) -> Automaton a b
19+pure :: (a -> b) -> Automaton a b
19init :: b -> (a -> b -> b) -> Automaton a b
20init :: b -> (a -> b -> b) -> Automaton a b
20init' :: s -> (a -> s -> (b,s)) -> Automaton a b
21init' :: s -> (a -> s -> (b,s)) -> Automaton a b
21
22
22count :: Automaton a Int
23count :: Automaton a Int
23
24
24draggable :: Form -> Automaton (Bool,(Int,Int)) Form
25draggable :: Form -> Automaton (Bool,(Int,Int)) Form
26+
25−foo :: (a -> b) -> Automaton b c -> Automaton a c
27+(^>>) :: (a -> b) -> Automaton b c -> Automaton a c
26−bar :: Automaton a b -> (b -> c) -> Automaton a c
28+(>>^) :: Automaton a b -> (b -> c) -> Automaton a c
29+
30+(^<<) :: (b -> c) -> Automaton a b -> Automaton a c
31+(<<^) :: Automaton b c -> (a -> b) -> Automaton a c
27
32
28--}
33--}
29
34
30
35
31run (Automaton m0) input =
36run (Automaton m0) input =
32− lift fst $ foldp1 (\a (b, Automaton m) -> m a) m0 input
37+ lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
33
38
39+a1 >>> a2 =
34−compose (Automaton m1) (Automaton m2) =
40+ let { Automaton m1 = a1 ; Automaton m2 = a2 } in
35− Automaton
41+ Automaton
36 (\a -> let (b,m1') = m1 a in
42 (\a -> let (b,m1') = m1 a in
37− let (c,m2') = m2 b in (c, compose (Automaton m1') (Automaton m2')))
43+ let (c,m2') = m2 b in (c, Automaton m1' >>> Automaton m2'))
44+
45+a2 <<< a1 = a1 >>> a2
46+f ^>> a = pure f >>> a
47+a >>^ f = a >>> pure f
48+f ^<< a = a >>> pure f
49+a <<^ f = pure f >>> a
38
50
39combine autos =
51combine autos =
40− Automaton (\a -> let (bs,autos') = unzip $ map (\m -> m a) autos in
52+ Automaton (\a -> let (bs,autos') = unzip $ map (\m -> m a) autos in
41− (bs, combine autos'))
53+ (bs, combine autos'))
54+
55+pure f = Automaton (\x -> (f x, pure f))
56+init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
57+init' s step = Automaton (\a -> let (b,s') = step a s in (b , init s' step))
58+
59+count = init 0 (\_ c -> c + 1)
60+
61+
62+
63+{--- See the following papers for ideas on how to make this faster:
42
64
43−init s step =
65+- Functional Reactive Programming, Continued
44− Automaton (\a -> let s' = step a s in (s', init s' step))
66+- Causal commutative arrows and their optimization
45
67
46−init' s step =
68+Speeding things up is a really low priority. Language features and
47− Automaton (\a -> let (b,s') = step a s in (b, init s' step))
69+libraries with nice APIs and are way more important!
48
70
49−count = init 0 (\_ c -> c + 1)
71+--}