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
7import Signal (lift,foldp)
9import Maybe (Just, Nothing)
11data Automaton a b = Step (a -> (Automaton a b, b))
13-- Run an automaton on a given signal. The automaton steps forward
14-- whenever the input signal updates.
15run : Automaton a b -> b -> Signal a -> Signal b
17 let step a (Step f, _) = f a
18 in lift (\(x,y) -> y) <| foldp step (auto,base) inputs
20-- Step an automaton forward once with a given input.
21step : a -> Automaton a b -> (Automaton a b, b)
24-- Compose two automatons, chaining them together.
25(>>>) : Automaton a b -> Automaton b c -> Automaton a c
27 Step (\a -> let (f', b) = step a f
31-- Compose two automatons, chaining them together.
32(<<<) : Automaton b c -> Automaton a b -> Automaton a c
35-- Combine a list of automatons into a single automaton that produces a list.
36combine : [Automaton a b] -> Automaton a [b]
38 Step (\a -> let (autos', bs) = unzip $ map (step a) autos
39 in (combine autos', bs))
41-- Create an automaton with no memory. It just applies the given function to
43pure : (a -> b) -> Automaton a b
44pure f = Step (\x -> (pure f, f x))
46-- Create an automaton with state. Requires an initial state and a step
47-- function to step the state forward. For example, an automaton that counted
48-- how many steps it has taken would look like this:
50-- count = Automaton a Int
51-- count = state 0 (\\_ c -> c+1)
53-- It is a stateful automaton. The initial state is zero, and the step function
54-- increments the state on every step.
55state : b -> (a -> b -> b) -> Automaton a b
56state s f = Step (\x -> let s' = f x s
59-- Create an automaton with hidden state. Requires an initial state and a
60-- step function to step the state forward and produce an output.
61hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
62hiddenState s f = Step (\x -> let (s',out) = f x s
63 in (hiddenState s' f, out))
65-- Count the number of steps taken.
66count : Automaton a Int
67count = state 0 (\_ c -> c + 1)
69type Queue t = ([t],[t])
71enqueue x (en,de) = (x::en, de)
74 (en,[]) -> dequeue ([], reverse en)
75 (en,hd::tl) -> Just (hd, (en,tl))
77-- Computes the running average of the last `n` inputs.
78average : Int -> Automaton Float Float
80 let step n (ns,len,sum) =
81 if len == k then stepFull n (ns,len,sum)
82 else ((enqueue n ns, len+1, sum+n), (sum+n) / (len+1))
83 stepFull n (ns,len,sum) =
85 Nothing -> ((ns,len,sum), 0)
86 Just (m,ns') -> let sum' = sum + n - m
87 in ((enqueue n ns', len, sum'), sum' / len)
88 in hiddenState (empty,0,0) step
91{-- TODO(evancz): See the following papers for ideas on how to make this
92library faster and better:
94- Functional Reactive Programming, Continued
95- Causal commutative arrows and their optimization
97Speeding things up is a really low priority. Language features and
98libraries with nice APIs and are way more important!
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list