on aa7a67f help sign in
elk/libraries/Automaton.elm
85 lines 3.3 kB history raw
1-- This library is a way to package up dynamic behavior. It makes it easier to
2-- dynamically create dynamic components. See the [original release
3-- notes](/blog/announce/version-0.5.0.elm) on this library to get a feel for how
4-- it can be used.
5module Automaton where
7data Automaton a b = Automaton (a -> (b, Automaton a b))
9-- Run an automaton on a given signal. The automaton takes in ‘a’ values and returns ‘b’ values. The automaton steps forward whenever the input signal updates.
10run : Automaton a b -> Signal a -> Signal b
11run (Automaton m0) input =
12 lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
14-- Step an automaton forward once with a given input.
15step : Automaton a b -> a -> (b, Automaton a b)
16step (Automaton m) a = m a
19-- Compose two automatons, chaining them together.
20(>>>) : Automaton a b -> Automaton b c -> Automaton a c
21a1 >>> a2 =
22 let Automaton m1 = a1
23 Automaton m2 = a2
24 in Automaton (\a -> let (b,m1') = m1 a
25 (c,m2') = m2 b
26 in (c, m1' >>> m2'))
28-- Compose two automatons, chaining them together.
29(<<<) : Automaton b c -> Automaton a b -> Automaton a c
30a2 <<< a1 = a1 >>> a2
32-- Combine a list of automatons into a single automaton that produces a list.
33combine : [Automaton a b] -> Automaton a [b]
34combine autos =
35 Automaton (\a -> let (bs,autos') = unzip $ map (\(Automaton m) -> m a) autos in
36 (bs, combine autos'))
38-- Create an automaton with no memory. It just applies the given function to every input.
39pure : (a -> b) -> Automaton a b
40pure f = Automaton (\x -> (f x, pure f))
42-- Create an automaton with no memory. It just applies the given function to every input.
43init : b -> (a -> b -> b) -> Automaton a b
44init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
46-- Create an automaton with hidden state. Requires an initial state and a step function to step the state forward and produce an output.
47init' : s -> (a -> s -> (b,s)) -> Automaton a b
48init' s step = Automaton (\a -> let (b,s') = step a s in (b , init' s' step))
50-- Count the number of steps taken.
51count : Automaton a Int
52count = init 0 (\_ c -> c + 1)
55data DragState = Listen | Ignore | DragFrom (Int,Int)
57vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
59stepDrag (press,pos) (ds,form) =
60 let wrap ds' = (form, (ds',form)) in
61 case ds of
62 Listen -> wrap (if | not press -> Listen
63 | pos `isWithin` form -> DragFrom pos
64 | otherwise -> Ignore)
65 Ignore -> wrap (if press then Ignore else Listen)
66 DragFrom p0 ->
67 if press then (uncurry move (vecSub pos p0) form, (DragFrom p0, form))
68 else (let form' = uncurry move (vecSub pos p0) form in
69 (form', (Listen,form')))
71-- Create a draggable form that can be dynamically created and added to a scene.
72draggable : Form -> Automaton (Bool,(Int,Int)) Form
73draggable form = init' (Listen,form) stepDrag
76{--- See the following papers for ideas on how to make this faster:
78- Functional Reactive Programming, Continued
79- Causal commutative arrows and their optimization
81Speeding things up is a really low priority. Language features and
82libraries with nice APIs and are way more important!
84--}