on master help sign in
Merge pull request #246 from jsl/update_automaton_docs

Convert Automaton docs

commitab7a1d74ce44285df41d6f224710c7e51220dc40 authorEvan Czaplicki <info@elm-lang.org> dateThu 12 Sep 2013, 14:27 (13 years ago) parent9e17f1317ee840
1 file changed+47−26
libraries/Automaton.elm+47−26
libraries/Automaton.elm+47−26
@@ -1,7 +1,22 @@
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.
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.
5+
6+# Running and stepping forward Automatons
7+@docs run, step
8+
9+# Putting Automatons together
10+@docs (>>>), (<<<)
11+
12+# Creating Automatons
13+@docs combine, pure, state, hiddenState
14+
15+# Measuring Automatons
16+@docs count, average
17+
18+-}
19+
520module Automaton where
621
722import open Basics
@@ -11,59 +26,66 @@ import Maybe (Just, Nothing)
1126
1227data Automaton a b = Step (a -> (Automaton a b, b))
1328
14−-- Run an automaton on a given signal. The automaton steps forward
15−-- whenever the input signal updates.
29+{-| Run an automaton on a given signal. The automaton steps forward whenever the
30+input signal updates.
31+-}
1632run : Automaton a b -> b -> Signal a -> Signal b
1733run auto base inputs =
1834 let step a (Step f, _) = f a
1935 in lift (\(x,y) -> y) (foldp step (auto,base) inputs)
2036
21−-- Step an automaton forward once with a given input.
37+{-| Step an automaton forward once with a given input. -}
2238step : a -> Automaton a b -> (Automaton a b, b)
2339step a (Step f) = f a
2440
25−-- Compose two automatons, chaining them together.
41+{-| Compose two automatons, chaining them together. -}
2642(>>>) : Automaton a b -> Automaton b c -> Automaton a c
2743f >>> g =
2844 Step (\a -> let (f', b) = step a f
2945 (g', c) = step b g
3046 in (f' >>> g', c))
3147
32−-- Compose two automatons, chaining them together.
48+{-| Compose two automatons, chaining them together. -}
3349(<<<) : Automaton b c -> Automaton a b -> Automaton a c
3450g <<< f = f >>> g
3551
36−-- Combine a list of automatons into a single automaton that produces a list.
52+{-| Combine a list of automatons into a single automaton that produces a
53+list.
54+-}
3755combine : [Automaton a b] -> Automaton a [b]
3856combine autos =
3957 Step (\a -> let (autos', bs) = unzip (map (step a) autos)
4058 in (combine autos', bs))
4159
42−-- Create an automaton with no memory. It just applies the given function to
43−-- every input.
60+{-| Create an automaton with no memory. It just applies the given function to
61+every input.
62+-}
4463pure : (a -> b) -> Automaton a b
4564pure f = Step (\x -> (pure f, f x))
4665
47−-- Create an automaton with state. Requires an initial state and a step
48−-- function to step the state forward. For example, an automaton that counted
49−-- how many steps it has taken would look like this:
50−--
51−-- count = Automaton a Int
52−-- count = state 0 (\\_ c -> c+1)
53−--
54−-- It is a stateful automaton. The initial state is zero, and the step function
55−-- increments the state on every step.
66+
67+{-| Create an automaton with state. Requires an initial state and a step
68+function to step the state forward. For example, an automaton that counted
69+how many steps it has taken would look like this:
70+
71+ count = Automaton a Int
72+ count = state 0 (\\_ c -> c+1)
73+
74+It is a stateful automaton. The initial state is zero, and the step function
75+increments the state on every step.
76+-}
5677state : b -> (a -> b -> b) -> Automaton a b
5778state s f = Step (\x -> let s' = f x s
5879 in (state s' f, s'))
5980
60−-- Create an automaton with hidden state. Requires an initial state and a
61−-- step function to step the state forward and produce an output.
81+{-| Create an automaton with hidden state. Requires an initial state and a
82+step function to step the state forward and produce an output.
83+-}
6284hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
6385hiddenState s f = Step (\x -> let (s',out) = f x s
6486 in (hiddenState s' f, out))
6587
66−-- Count the number of steps taken.
88+{-| Count the number of steps taken. -}
6789count : Automaton a Int
6890count = state 0 (\_ c -> c + 1)
6991
@@ -75,7 +97,7 @@ dequeue q = case q of
7597 (en,[]) -> dequeue ([], reverse en)
7698 (en,hd::tl) -> Just (hd, (en,tl))
7799
78−-- Computes the running average of the last `n` inputs.
100+{-| Computes the running average of the last `n` inputs. -}
79101average : Int -> Automaton Float Float
80102average k =
81103 let step n (ns,len,sum) =
@@ -98,4 +120,3 @@ library faster and better:
98120Speeding things up is a really low priority. Language features and
99121libraries with nice APIs and are way more important!
100122--}
101−