on master help sign in
Merge pull request #243 from mgold/dev

Convert Date, Mouse, Window, and Signal docs.

commitd6d0ba767d4ace40f403476c3ca2efd9bba1815c authorEvan Czaplicki <info@elm-lang.org> dateSun 8 Sep 2013, 13:58 (13 years ago) parentf11925e6869015
4 files changed+146−95
libraries/Date.elm+32−24 libraries/Mouse.elm+19−9 libraries/Signal.elm+82−58 libraries/Window.elm+13−4
libraries/Date.elm+32−24
@@ -1,9 +1,17 @@
11
2−-- Library for working with dates. It is still a work in progress, so email
3−-- the mailing list if you are having issues with internationalization or
4−-- locale formatting or something.
52module Date where
63
4+{-| Library for working with dates. Email the mailing list if you encounter
5+issues with internationalization or locale formatting.
6+
7+# Conversions
8+@docs read, toTime
9+
10+# Extractions
11+@docs year, month, Month, day, dayOfWeek, Day, hour, minute, second
12+
13+-}
14+
715import Basics (String)
816import Native.Date
917import Time (Time)
@@ -11,55 +19,55 @@ import Maybe (Maybe)
1119
1220data Date = Date
1321
14−-- Represents the days of the week.
22+{-| Represents the days of the week. -}
1523data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
1624
17−-- Represents the month of the year.
25+{-| Represents the month of the year. -}
1826data Month = Jan | Feb | Mar | Apr
1927 | May | Jun | Jul | Aug
2028 | Sep | Oct | Nov | Dec
2129
22−-- Attempt to read a date from a string.
30+{-| Attempt to read a date from a string. -}
2331read : String -> Maybe Date
2432read = Native.Date.read
2533
26−-- Convert a date into a time since midnight (UTC) of 1 January 1990 (i.e.
27−-- [UNIX time](http://en.wikipedia.org/wiki/Unix_time)). Given the date 23 June
28−-- 1990 at 11:45AM this returns the corresponding time.
34+{-| Convert a date into a time since midnight (UTC) of 1 January 1990 (i.e.
35+[UNIX time](http://en.wikipedia.org/wiki/Unix_time)). Given the date 23 June
36+1990 at 11:45AM this returns the corresponding time. -}
2937toTime : Date -> Time
3038toTime = Native.Date.toTime
3139
32−-- Extract the year of a given date. Given the date 23 June 1990 at 11:45AM
33−-- this returns the integer `1990`.
40+{-| Extract the year of a given date. Given the date 23 June 1990 at 11:45AM
41+this returns the integer `1990`. -}
3442year : Date -> Int
3543year = Native.Date.year
3644
37−-- Extract the month of a given date. Given the date 23 June 1990 at 11:45AM
38−-- this returns the Month `Jun` as defined below.
45+{-| Extract the month of a given date. Given the date 23 June 1990 at 11:45AM
46+this returns the Month `Jun` as defined below. -}
3947month : Date -> Month
4048month = Native.Date.month
4149
42−-- Extract the day of a given date. Given the date 23 June 1990 at 11:45AM
43−-- this returns the integer `23`.
50+{-| Extract the day of a given date. Given the date 23 June 1990 at 11:45AM
51+this returns the integer `23`. -}
4452day : Date -> Int
4553day = Native.Date.day
4654
47−-- Extract the day of the week for a given date. Given the date 23 June
48−-- 1990 at 11:45AM this returns the Day `Thu` as defined below.
55+{-| Extract the day of the week for a given date. Given the date 23 June
56+1990 at 11:45AM this returns the Day `Thu` as defined below. -}
4957dayOfWeek : Date -> Day
5058dayOfWeek = Native.Date.dayOfWeek
5159
52−-- Extract the hour of a given date. Given the date 23 June 1990 at 11:45AM
53−-- this returns the integer `11`.
60+{-| Extract the hour of a given date. Given the date 23 June 1990 at 11:45AM
61+this returns the integer `11`. -}
5462hour : Date -> Int
5563hour = Native.Date.hour
5664
57−-- Extract the minute of a given date. Given the date 23 June 1990 at 11:45AM
58−-- this returns the integer `45`.
65+{-| Extract the minute of a given date. Given the date 23 June 1990 at 11:45AM
66+this returns the integer `45`. -}
5967minute : Date -> Int
6068minute = Native.Date.minute
6169
62−-- Extract the second of a given date. Given the date 23 June 1990 at 11:45AM
63−-- this returns the integer `0`.
70+{-| Extract the second of a given date. Given the date 23 June 1990 at 11:45AM
71+this returns the integer `0`. -}
6472second : Date -> Int
65−second = Native.Date.second
73+second = Native.Date.second
libraries/Mouse.elm+19−9
@@ -1,31 +1,41 @@
11
22module Mouse where
33
4+{-| Library for working with mouse input.
5+
6+# Position
7+@docs position, x, y
8+
9+# Button Status
10+@docs isDown, clicks, isClicked
11+
12+-}
13+
414import Signal (Signal)
515import Native.Mouse
616
7−-- The current mouse position.
17+{-| The current mouse position. -}
818position : Signal (Int,Int)
919position = Native.Mouse.position
1020
11−-- The current x-coordinate of the mouse.
21+{-| The current x-coordinate of the mouse. -}
1222x : Signal Int
1323x = Native.Mouse.x
1424
15−-- The current y-coordinate of the mouse.
25+{-| The current y-coordinate of the mouse. -}
1626y : Signal Int
1727y = Native.Mouse.y
1828
19−-- The current state of the left mouse-button.
20−-- True when the button is down, and false otherwise.
29+{-| The current state of the left mouse-button.
30+True when the button is down, and false otherwise. -}
2131isDown : Signal Bool
2232isDown = Native.Mouse.isDown
2333
24−-- True immediately after the left mouse-button has been clicked,
25−-- and false otherwise.
34+{-| True immediately after the left mouse-button has been clicked,
35+and false otherwise. -}
2636isClicked : Signal Bool
2737isClicked = Native.Mouse.isClicked
2838
29−-- Always equal to unit. Event triggers on every mouse click.
39+{-| Always equal to unit. Event triggers on every mouse click. -}
3040clicks : Signal ()
31−clicks = Native.Mouse.clicks
41+clicks = Native.Mouse.clicks
libraries/Signal.elm+82−58
@@ -1,32 +1,52 @@
11
2−-- The library for general signal manipulation. Some useful functions for
3−-- working with time (e.g. setting FPS) and combining signals and time (e.g.
4−-- delaying updates, getting timestamps) can be found in the
5−-- [`Time`](/docs/Signal/Time.elm) library.
6−--
7−-- Note: There are lift functions up to `lift8`.
2+{-| The library for general signal manipulation. Includes lift functions up to
3+`lift8` and infix lift operators `<~` and `~`, combinations, filters, and
4+past-dependence.
5+
6+Signals are time-varying values. Lifted functions are reevaluated whenver any of
7+their input signals has an event. Signal events may be of the same value as the
8+previous value of the signal. Such signals are useful for timing and
9+past-dependence.
10+
11+Some useful functions for working with time (e.g. setting FPS) and combining
12+signals and time (e.g. delaying updates, getting timestamps) can be found in
13+the [`Time`](/docs/Signal/Time.elm) library.
14+
15+# Combine
16+@docs constant, lift, lift2, merge, merges, combine
17+
18+# Pretty Lift
19+@docs (<~), (~)
20+
21+# Past-Dependence
22+@docs foldp, count, countIf
23+
24+#Filters
25+@docs keepIf, dropIf, keepWhen, dropWhen, dropRepeats, sampleOn
26+
27+# Do you even lift?
28+@docs lift3, lift4, lift5, lift6, lift7, lift8
29+
30+-}
831module Signal where
932
1033import Native.Signal
1134import List (foldr)
1235
1336data Signal a = Signal
14−-- = Constant a
15−-- | Lift (a -> a) (Signal a)
1637
17−-- Create a constant signal that never changes.
38+{-| Create a constant signal that never changes. -}
1839constant : a -> Signal a
1940constant = Native.Signal.constant
2041
21−-- Transform a signal with a given function.
42+{-| Transform a signal with a given function. -}
2243lift : (a -> b) -> Signal a -> Signal b
2344lift = Native.Signal.lift
2445
25−-- Combine two signals with a given function.
46+{-| Combine two signals with a given function. -}
2647lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
2748lift2 = Native.Signal.lift2
2849
29−-- Combine three signals with a given function.
3050lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
3151lift3 = Native.Signal.lift3
3252
@@ -49,25 +69,25 @@ lift8 : (a -> b -> c -> d -> e -> f -> g -> h -> i)
4969lift8 = Native.Signal.lift8
5070
5171
52−-- Create a past-dependent signal. Each value given on the input signal will
53−-- be accumulated, producing a new output value.
54−--
55−-- For instance, `(foldp (\\arrows number -> number + arrows.y) 0 Keyboard.arrows)`
56−-- increments or decrements the accumulated value (which starts at zero) when the up or down arrow keys are pressed.
72+{-| Create a past-dependent signal. Each value given on the input signal will
73+be accumulated, producing a new output value.
74+
75+For instance, `foldp (+) 0 (fps 40)` is the time the program has been running,
76+updated 40 times a second. -}
5777foldp : (a -> b -> b) -> b -> Signal a -> Signal b
5878foldp = Native.Signal.foldp
5979
60−-- Merge two signals into one, biased towards the first signal if both signals
61−-- update at the same time.
80+{-| Merge two signals into one, biased towards the first signal if both signals
81+update at the same time. -}
6282merge : Signal a -> Signal a -> Signal a
6383merge = Native.Signal.merge
6484
65−-- Merge many signals into one, biased towards the left-most signal if multiple
66−-- signals update simultaneously.
85+{-| Merge many signals into one, biased towards the left-most signal if multiple
86+signals update simultaneously. -}
6787merges : [Signal a] -> Signal a
6888merges = Native.Signal.merges
6989
70−-- Combine a list of signals into a signal of lists.
90+{-| Combine a list of signals into a signal of lists. -}
7191combine : [Signal a] -> Signal [a]
7292combine = foldr (Native.Signal.lift2 (::)) (Native.Signal.constant [])
7393
@@ -76,70 +96,74 @@ combine = foldr (Native.Signal.lift2 (::)) (Native.Signal.constant [])
7696 -- fold over non-homogeneous inputs.
7797 -- mergeEither : Signal a -> Signal b -> Signal (Either a b)
7898
79−-- Count the number of events that have occured.
99+{-| Count the number of events that have occured. -}
80100count : Signal a -> Signal Int
81101count = Native.Signal.count
82102
83−-- Count the number of events that have occured that satisfy a given predicate.
103+{-| Count the number of events that have occured that satisfy a given predicate.
104+-}
84105countIf : (a -> Bool) -> Signal a -> Signal Int
85106countIf = Native.Signal.countIf
86107
87−-- Keep only events that satisfy the given predicate. Elm does not allow
88−-- undefined signals, so a base case must be provided in case the predicate is
89−-- never satisfied.
108+{-| Keep only events that satisfy the given predicate. Elm does not allow
109+undefined signals, so a base case must be provided in case the predicate is
110+never satisfied. -}
90111keepIf : (a -> Bool) -> a -> Signal a -> Signal a
91112keepIf = Native.Signal.keepIf
92113
93−-- Drop events that satisfy the given predicate. Elm does not allow undefined
94−-- signals, so a base case must be provided in case the predicate is never
95−-- satisfied.
114+{-| Drop events that satisfy the given predicate. Elm does not allow undefined
115+signals, so a base case must be provided in case the predicate is never
116+satisfied. -}
96117dropIf : (a -> Bool) -> a -> Signal a -> Signal a
97118dropIf = Native.Signal.dropIf
98119
99−-- Keep events only when the first signal is true. When the first signal becomes
100−-- true, the most recent value of the second signal will be propagated. Until
101−-- the first signal becomes false again, all events will be propagated. Elm does
102−-- not allow undefined signals, so a base case must be provided in case the first
103−-- signal is never true.
120+{-| Keep events only when the first signal is true. When the first signal
121+becomes true, the most recent value of the second signal will be propagated.
122+Until the first signal becomes false again, all events will be propagated. Elm
123+does not allow undefined signals, so a base case must be provided in case the
124+first signal is never true. -}
104125keepWhen : Signal Bool -> a -> Signal a -> Signal a
105126keepWhen = Native.Signal.keepWhen
106127
107−-- Drop events when the first signal is true. When the first signal becomes false,
108−-- the most recent value of the second signal will be propagated. Until the first
109−-- signal becomes true again, all events will be propagated. Elm does not allow
110−-- undefined signals, so a base case must be provided in case the first signal is
111−-- always true.
128+{-| Drop events when the first signal is true. When the first signal becomes
129+false, the most recent value of the second signal will be propagated. Until the
130+first signal becomes true again, all events will be propagated. Elm does not
131+allow undefined signals, so a base case must be provided in case the first
132+signal is always true. -}
112133dropWhen : Signal Bool -> a -> Signal a -> Signal a
113134dropWhen = Native.Signal.dropWhen
114135
115−-- Drop sequential repeated values. For example, if a signal produces the
116−-- sequence `[1,1,2,2,1]`, it becomes `[1,2,1]` by dropping the values that
117−-- are the same as the previous value.
136+{-| Drop sequential repeated values. For example, if a signal produces the
137+sequence `[1,1,2,2,1]`, it becomes `[1,2,1]` by dropping the values that are the
138+same as the previous value. -}
118139dropRepeats : Signal a -> Signal a
119140dropRepeats = Native.Signal.dropRepeats
120141
121−-- Sample from the second input every time an event occurs on the first input.
122−-- For example, `(sampleOn clicks (every second))` will give the approximate
123−-- time of the latest click.
142+{-| Sample from the second input every time an event occurs on the first input.
143+For example, `(sampleOn clicks (every second))` will give the approximate time
144+of the latest click. -}
124145sampleOn : Signal a -> Signal b -> Signal b
125146sampleOn = Native.Signal.sampleOn
126147
127−-- An alias for `lift`. A prettier way to apply a
128−-- function to the current value of a signal.
129−--
130−-- lift f signal == f <~ signal
148+{-| An alias for `lift`. A prettier way to apply a function to the current value
149+of a signal. -}
131150(<~) : (a -> b) -> Signal a -> Signal b
132151f <~ s = Native.Signal.lift f s
133152
134−-- Signal application. This takes two signals, holding a function and
135−-- a value. It applies the current function to the current value.
136−--
137−-- So the following expressions are equivalent:
138−--
139−-- scene <~ Window.dimensions ~ Mouse.position
140−-- lift2 scene Window.dimensions Mouse.position
153+{-| Informally, an alias for `liftN`. Intersperse it between additional signal
154+arguments of the lifted function.
155+
156+Formally, signal application. This takes two signals, holding a function and
157+a value. It applies the current function to the current value.
158+
159+The following expressions are equivalent:
160+
161+ scene <~ Window.dimensions ~ Mouse.position
162+ lift2 scene Window.dimensions Mouse.position
163+-}
164+
141165(~) : Signal (a -> b) -> Signal a -> Signal b
142166sf ~ s = Native.Signal.lift2 (\f x -> f x) sf s
143167
144168infixl 4 <~
145−infixl 4 ~
169+infixl 4 ~
libraries/Window.elm+13−4
@@ -1,18 +1,27 @@
11
22module Window where
33
4+{-| Provides information about the container that your Elm program lives in.
5+When you embed Elm in a `<div>` it gives the dimensions of the container, not
6+the whole window.
7+
8+# Dimensions
9+@docs dimensions, width, height
10+
11+-}
12+
413import Signal (Signal)
514import Native.Window
615
7−-- The current dimensions of the window (i.e. the area viewable to the
8−-- user, not including scroll bars).
16+{-| The current width and height of the window (i.e. the area viewable to the
17+user, not including scroll bars). -}
918dimensions : Signal (Int,Int)
1019dimensions = Native.Window.dimensions
1120
12−-- The current width of the window.
21+{-| The current width of the window. -}
1322width : Signal Int
1423width = Native.Window.width
1524
16−-- The current height of the window.
25+{-| The current height of the window. -}
1726height : Signal Int
1827height = Native.Window.height