on master help sign in
Merge pull request #127 from madscoaducom/docs-update
d2acbcaEvan Czaplicki
libraries/Automaton.elm+16−2
@@ -1,16 +1,22 @@
1−
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.
2module Automaton where
5module Automaton where
3
6
4data Automaton a b = Automaton (a -> (b, Automaton a b))
7data Automaton a b = Automaton (a -> (b, Automaton a b))
5
8
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.
6run : Automaton a b -> Signal a -> Signal b
10run : Automaton a b -> Signal a -> Signal b
7run (Automaton m0) input =
11run (Automaton m0) input =
8 lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
12 lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
9
13
14+-- Step an automaton forward once with a given input.
10step : Automaton a b -> a -> (b, Automaton a b)
15step : Automaton a b -> a -> (b, Automaton a b)
11step (Automaton m) a = m a
16step (Automaton m) a = m a
12
17
13
18
19+-- Compose two automatons, chaining them together.
14(>>>) : Automaton a b -> Automaton b c -> Automaton a c
20(>>>) : Automaton a b -> Automaton b c -> Automaton a c
15a1 >>> a2 =
21a1 >>> a2 =
16 let Automaton m1 = a1
22 let Automaton m1 = a1
@@ -19,23 +25,29 @@ a1 >>> a2 =
19 (c,m2') = m2 b
25 (c,m2') = m2 b
20 in (c, m1' >>> m2'))
26 in (c, m1' >>> m2'))
21
27
28+-- Compose two automatons, chaining them together.
22(<<<) : Automaton b c -> Automaton a b -> Automaton a c
29(<<<) : Automaton b c -> Automaton a b -> Automaton a c
23a2 <<< a1 = a1 >>> a2
30a2 <<< a1 = a1 >>> a2
24
31
32+-- Combine a list of automatons into a single automaton that produces a list.
25combine : [Automaton a b] -> Automaton a [b]
33combine : [Automaton a b] -> Automaton a [b]
26combine autos =
34combine autos =
27 Automaton (\a -> let (bs,autos') = unzip $ map (\(Automaton m) -> m a) autos in
35 Automaton (\a -> let (bs,autos') = unzip $ map (\(Automaton m) -> m a) autos in
28 (bs, combine autos'))
36 (bs, combine autos'))
29
37
38+-- Create an automaton with no memory. It just applies the given function to every input.
30pure : (a -> b) -> Automaton a b
39pure : (a -> b) -> Automaton a b
31pure f = Automaton (\x -> (f x, pure f))
40pure f = Automaton (\x -> (f x, pure f))
32
41
42+-- Create an automaton with no memory. It just applies the given function to every input.
33init : b -> (a -> b -> b) -> Automaton a b
43init : b -> (a -> b -> b) -> Automaton a b
34init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
44init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
35
45
46+-- Create an automaton with hidden state. Requires an initial state and a step function to step the state forward and produce an output.
36init' : s -> (a -> s -> (b,s)) -> Automaton a b
47init' : s -> (a -> s -> (b,s)) -> Automaton a b
37init' s step = Automaton (\a -> let (b,s') = step a s in (b , init' s' step))
48init' s step = Automaton (\a -> let (b,s') = step a s in (b , init' s' step))
38
49
50+-- Count the number of steps taken.
39count : Automaton a Int
51count : Automaton a Int
40count = init 0 (\_ c -> c + 1)
52count = init 0 (\_ c -> c + 1)
41
53
@@ -56,6 +68,7 @@ stepDrag (press,pos) (ds,form) =
56 else (let form' = uncurry move (vecSub pos p0) form in
68 else (let form' = uncurry move (vecSub pos p0) form in
57 (form', (Listen,form')))
69 (form', (Listen,form')))
58
70
71+-- Create a draggable form that can be dynamically created and added to a scene.
59draggable : Form -> Automaton (Bool,(Int,Int)) Form
72draggable : Form -> Automaton (Bool,(Int,Int)) Form
60draggable form = init' (Listen,form) stepDrag
73draggable form = init' (Listen,form) stepDrag
61
74
@@ -68,4 +81,5 @@ draggable form = init' (Listen,form) stepDrag
68Speeding things up is a really low priority. Language features and
81Speeding things up is a really low priority. Language features and
69libraries with nice APIs and are way more important!
82libraries with nice APIs and are way more important!
70
83
71−--}
84+--}
85+
libraries/Date.elm+32−1
@@ -1,19 +1,50 @@
1
1
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.
2module Date where
5module Date where
3
6
7+-- Represents the days of the week.
4data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
8data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
5
9
10+-- Represents the month of the year.
6data Month = Jan | Feb | Mar | Apr
11data Month = Jan | Feb | Mar | Apr
7 | May | Jun | Jul | Aug
12 | May | Jun | Jul | Aug
8 | Sep | Oct | Nov | Dec
13 | Sep | Oct | Nov | Dec
9
14
15+-- Attempt to read a date from a string.
10−read : String -> Date
16+read : String -> Maybe Date
17+
18+-- Convert a date into a time since midnight (UTC) of 1 January 1990 (i.e.
19+-- [UNIX time](http://en.wikipedia.org/wiki/Unix_time)). Given the date 23 June
20+-- 1990 at 11:45AM this returns the corresponding time.
11toTime : Date -> Time
21toTime : Date -> Time
12
22
23+-- Extract the year of a given date. Given the date 23 June 1990 at 11:45AM
24+-- this returns the integer `1990`.
13year : Date -> Int
25year : Date -> Int
26+
27+-- Extract the month of a given date. Given the date 23 June 1990 at 11:45AM
28+-- this returns the Month `Jun` as defined below.
14month : Date -> Month
29month : Date -> Month
30+
31+
32+-- Extract the day of a given date. Given the date 23 June 1990 at 11:45AM
33+-- this returns the integer `23`.
15day : Date -> Int
34day : Date -> Int
35+
36+-- Extract the day of the week for a given date. Given the date 23 June
37+-- 1990 at 11:45AM this returns the Day `Thu` as defined below.
16dayOfWeek : Date -> Day
38dayOfWeek : Date -> Day
39+
40+-- Extract the hour of a given date. Given the date 23 June 1990 at 11:45AM
41+-- this returns the integer `11`.
17hour : Date -> Int
42hour : Date -> Int
43+
44+-- Extract the minute of a given date. Given the date 23 June 1990 at 11:45AM
45+-- this returns the integer `45`.
18minute : Date -> Int
46minute : Date -> Int
47+
48+-- Extract the second of a given date. Given the date 23 June 1990 at 11:45AM
49+-- this returns the integer `0`.
19second : Date -> Int
50second : Date -> Int
libraries/Dict.elm+19−1
@@ -16,6 +16,7 @@ data NColor = Red | Black
16
16
17data Dict k v = Node NColor k v (Dict k v) (Dict k v) | Empty
17data Dict k v = Node NColor k v (Dict k v) (Dict k v) | Empty
18
18
19+-- Create an empty dictionary.
19empty : Dict k v
20empty : Dict k v
20empty = Empty
21empty = Empty
21
22
@@ -114,6 +115,7 @@ max t =
114 }
115 }
115--}
116--}
116
117
118+-- Lookup the value associated with a key.
117lookup : k -> Dict k v -> Maybe v
119lookup : k -> Dict k v -> Maybe v
118lookup k t =
120lookup k t =
119 case t of
121 case t of
@@ -124,6 +126,7 @@ lookup k t =
124 EQ -> Just v
126 EQ -> Just v
125 GT -> lookup k r
127 GT -> lookup k r
126
128
129+-- Find the value associated with a key. If the key is not found, return the default value.
127findWithDefault : v -> k -> Dict k v -> v
130findWithDefault : v -> k -> Dict k v -> v
128findWithDefault base k t =
131findWithDefault base k t =
129 case t of
132 case t of
@@ -135,6 +138,7 @@ findWithDefault base k t =
135 GT -> findWithDefault base k r
138 GT -> findWithDefault base k r
136
139
137{--
140{--
141+-- Find the value associated with a key. If the key is not found, there will be a runtime error.
138find k t =
142find k t =
139 case t of
143 case t of
140 { Empty -> Error.raise "Key was not found in dictionary!"
144 { Empty -> Error.raise "Key was not found in dictionary!"
@@ -146,8 +150,9 @@ find k t =
146 }
150 }
147--}
151--}
148
152
149−-- Does t contain k?
153+-- Determine if a key is in a dictionary.
150member : k -> Dict k v -> Bool
154member : k -> Dict k v -> Bool
155+-- Does t contain k?
151member k t = Maybe.isJust $ lookup k t
156member k t = Maybe.isJust $ lookup k t
152
157
153rotateLeft : Dict k v -> Dict k v
158rotateLeft : Dict k v -> Dict k v
@@ -201,6 +206,7 @@ ensureBlackRoot t =
201 Node Red k v l r -> Node Black k v l r
206 Node Red k v l r -> Node Black k v l r
202 _ -> t
207 _ -> t
203
208
209+-- Insert a key-value pair into a dictionary. Replaces value when there is a collision.
204-- Invariant: t is a valid left-leaning rb tree *)
210-- Invariant: t is a valid left-leaning rb tree *)
205insert : k -> v -> Dict k v -> Dict k v
211insert : k -> v -> Dict k v -> Dict k v
206insert k v t =
212insert k v t =
@@ -223,6 +229,7 @@ insert k v t =
223 else new_t)
229 else new_t)
224--}
230--}
225
231
232+-- Create a dictionary with one key-value pair.
226singleton : k -> v -> Dict k v
233singleton : k -> v -> Dict k v
227singleton k v = insert k v Empty
234singleton k v = insert k v Empty
228
235
@@ -303,6 +310,7 @@ deleteMax t =
303 in ensureBlackRoot (del t)
310 in ensureBlackRoot (del t)
304--}
311--}
305
312
313+-- Remove a key-value pair from a dictionary. If the key is not found, no changes are made.
306remove : k -> Dict k v -> Dict k v
314remove : k -> Dict k v -> Dict k v
307remove k t =
315remove k t =
308 let eq_and_noRightNode t =
316 let eq_and_noRightNode t =
@@ -335,43 +343,53 @@ remove k t =
335 Error.raise "invariants broken after remove")
343 Error.raise "invariants broken after remove")
336--}
344--}
337
345
346+-- Apply a function to all values in a dictionary.
338map : (a -> b) -> Dict k a -> Dict k b
347map : (a -> b) -> Dict k a -> Dict k b
339map f t =
348map f t =
340 case t of
349 case t of
341 Empty -> Empty
350 Empty -> Empty
342 Node c k v l r -> Node c k (f v) (map f l) (map f r)
351 Node c k v l r -> Node c k (f v) (map f l) (map f r)
343
352
353+-- Fold over the key-value pairs in a dictionary, in order from lowest key to highest key.
344foldl : (k -> v -> b -> b) -> b -> Dict k v -> b
354foldl : (k -> v -> b -> b) -> b -> Dict k v -> b
345foldl f acc t =
355foldl f acc t =
346 case t of
356 case t of
347 Empty -> acc
357 Empty -> acc
348 Node _ k v l r -> foldl f (f k v (foldl f acc l)) r
358 Node _ k v l r -> foldl f (f k v (foldl f acc l)) r
349
359
360+-- Fold over the key-value pairs in a dictionary, in order from highest key to lowest key.
350foldr : (k -> v -> b -> b) -> b -> Dict k v -> b
361foldr : (k -> v -> b -> b) -> b -> Dict k v -> b
351foldr f acc t =
362foldr f acc t =
352 case t of
363 case t of
353 Empty -> acc
364 Empty -> acc
354 Node _ k v l r -> foldr f (f k v (foldr f acc r)) l
365 Node _ k v l r -> foldr f (f k v (foldr f acc r)) l
355
366
367+-- Combine two dictionaries. If there is a collision, preference is given to the first dictionary.
356union : Dict k v -> Dict k v -> Dict k v
368union : Dict k v -> Dict k v -> Dict k v
357union t1 t2 = foldl insert t2 t1
369union t1 t2 = foldl insert t2 t1
358
370
371+-- Keep a key-value pair when its key appears in the second dictionary. Preference is given to values in the first dictionary.
359intersect : Dict k v -> Dict k v -> Dict k v
372intersect : Dict k v -> Dict k v -> Dict k v
360intersect t1 t2 =
373intersect t1 t2 =
361 let combine k v t = if k `member` t2 then insert k v t else t
374 let combine k v t = if k `member` t2 then insert k v t else t
362 in foldl combine empty t1
375 in foldl combine empty t1
363
376
377+-- Keep a key-value pair when its key does not appear in the second dictionary. Preference is given to the first dictionary.
364diff : Dict k v -> Dict k v -> Dict k v
378diff : Dict k v -> Dict k v -> Dict k v
365diff t1 t2 = foldl (\k v t -> remove k t) t1 t2
379diff t1 t2 = foldl (\k v t -> remove k t) t1 t2
366
380
381+-- Get all of the keys in a dictionary.
367keys : Dict k v -> [k]
382keys : Dict k v -> [k]
368keys t = foldr (\k v acc -> k :: acc) [] t
383keys t = foldr (\k v acc -> k :: acc) [] t
369
384
385+-- Get all of the values in a dictionary.
370values : Dict k v -> [v]
386values : Dict k v -> [v]
371values t = foldr (\k v acc -> v :: acc) [] t
387values t = foldr (\k v acc -> v :: acc) [] t
372
388
389+-- Convert a dictionary into an association list of key-value pairs.
373toList : Dict k v -> [(k,v)]
390toList : Dict k v -> [(k,v)]
374toList t = foldr (\k v acc -> (k,v) :: acc) [] t
391toList t = foldr (\k v acc -> (k,v) :: acc) [] t
375
392
393+-- Convert an association list into a dictionary.
376fromList : [(k,v)] -> Dict k v
394fromList : [(k,v)] -> Dict k v
377fromList assocs = List.foldl (uncurry insert) empty assocs
395fromList assocs = List.foldl (uncurry insert) empty assocs
libraries/Either.elm+14−4
@@ -3,24 +3,34 @@ module Either where
3
3
4import List
4import List
5
5
6+-- Represents any data that can take two different types.
7+--
8+-- This can also be used for error handling `(Either String a)` where error
9+-- messages are stored on the left, and the correct values (&ldquo;right&rdquo; values) are stored on the right.
6data Either a b = Left a | Right b
10data Either a b = Left a | Right b
7
11
12+-- Apply the first function to a `Left` and the second function to a `Right`.
13+-- This allows the extraction of a value from an `Either`.
8either : (a -> c) -> (b -> c) -> Either a b -> c
14either : (a -> c) -> (b -> c) -> Either a b -> c
9either f g e = case e of { Left x -> f x ; Right y -> g y }
15either f g e = case e of { Left x -> f x ; Right y -> g y }
10
16
11−
17+-- True if the value is a `Left`.
12isLeft : Either a b -> Bool
18isLeft : Either a b -> Bool
13isLeft e = case e of { Left _ -> True ; _ -> False }
19isLeft e = case e of { Left _ -> True ; _ -> False }
14
20
21+-- True if the value is a `Right`.
15isRight : Either a b -> Bool
22isRight : Either a b -> Bool
16isRight e = case e of { Right _ -> True ; _ -> False }
23isRight e = case e of { Right _ -> True ; _ -> False }
17
24
18−
25+-- Keep only the values held in `Left` values.
19--lefts : [Either a b] -> [a]
26--lefts : [Either a b] -> [a]
20lefts es = List.filter isLeft es
27lefts es = List.filter isLeft es
21
28
29+-- Keep only the values held in `Right` values.
22--rights : [Either a b] -> [b]
30--rights : [Either a b] -> [b]
23rights es = List.filter isRight es
31rights es = List.filter isRight es
24
32
33+-- Split into two lists, lefts on the left and rights on the right. So we
34+-- have the equivalence: `(partition es == (lefts es, rights es))`
25−--partition : [Either a b] -> ([a],[b])
35+-- partition : [Either a b] -> ([a],[b])
26−partition es = List.partition isLeft es
36+partition es = List.partition isLeft es
libraries/Graphics/Color.elm+17
@@ -11,7 +11,10 @@ module Graphics.Color (rgba,rgb,hsva,hsv,
11
11
12data Color = Color Int Int Int Float
12data Color = Color Int Int Int Float
13
13
14+-- Create RGB colors with an alpha component for transparency. The alpha component is specified with numbers between 0 and 1.
14rgba = Color
15rgba = Color
16+
17+-- Create RGB colors from numbers between 0 and 255 inclusive.
15rgb r g b = Color r g b 1
18rgb r g b = Color r g b 1
16
19
17red = Color 255 0 0 1
20red = Color 255 0 0 1
@@ -38,8 +41,22 @@ purple = Color 128 0 128 1
38forestGreen = Color 34 139 34 1
41forestGreen = Color 34 139 34 1
39violet = Color 238 130 238 1
42violet = Color 238 130 238 1
40
43
44+-- Produce a &ldquo;complementary color&rdquo;. The two colors will accent each other.
41complement : Color -> Color
45complement : Color -> Color
46+
47+-- Create HSV colors with an alpha component for transparency. The alpha component is specified with numbers between 0 and 1.
42hsva : Int -> Float -> Float -> Float -> Color
48hsva : Int -> Float -> Float -> Float -> Color
49+
50+-- Create HSV colors. HSV stands for hue-saturation-value.
51+--
52+-- Hue is a degree from 0 to 360 representing a color wheel: red at 0&deg;, green at 120&deg;, blue at 240&deg;, and red again at 360&deg;.
53+-- This makes it easy to cycle through colors and compute color complements, triads, tetrads, etc.
54+--
55+-- Saturation is a number between 1 and 0 where lowering this number makes your color more grey. This can help you tone a color down.
56+--
57+-- Value is also a number between 1 and 0. Lowering this number makes your color more black.
58+--
59+-- Look up the &ldquo;HSV cylinder&rdquo; for more information.
43hsv : Int -> Float -> Float -> Color
60hsv : Int -> Float -> Float -> Color
44
61
45data Gradient
62data Gradient
libraries/Graphics/Text.elm+46−10
@@ -4,21 +4,57 @@ module Graphics.Text where
4import Native.Graphics.Text as T
4import Native.Graphics.Text as T
5
5
6
6
7+-- Convert a string into text which can be styled and displayed.
7toText : String -> Text
8toText : String -> Text
8
9
10+-- Set the typeface of some text. The first argument should be a comma separated listing of the desired typefaces
11+-- "helvetica, arial, sans-serif"
12+-- Works the same as the CSS font-family property.
9−typeface : String -> Text -> Text
13+typeface : String -> Text -> Text
14+
15+-- Switch to a monospace typeface. Good for code snippets.
16+monospace : Text -> Text
17+
18+-- Make text big and noticable.
19+header : Text -> Text
20+
21+-- Create a link.
10−href : String -> Text -> Text
22+href : String -> Text -> Text
23+
24+-- Set the height of text in \"ems\". 1em is the normal height of text. 2ems is twice that height.
11−height : Float -> Text -> Text
25+height : Float -> Text -> Text
26+
27+-- Set the color of a string.
12−color : Color -> Text -> Text
28+color : Color -> Text -> Text
29+
30+-- Make a string bold.
13−bold : Text -> Text
31+bold : Text -> Text
32+
33+-- Italicize a string.
14−italic : Text -> Text
34+italic : Text -> Text
35+
36+-- Draw a line above a string.
15−overline : Text -> Text
37+overline : Text -> Text
38+
39+-- Underline a string.
16underline : Text -> Text
40underline : Text -> Text
41+
42+-- Draw a line through a string.
17strikeThrough : Text -> Text
43strikeThrough : Text -> Text
18
44
45+-- Display justified, styled text.
19justified : Text -> Element
46justified : Text -> Element
20−centered : Text -> Element
21−righted : Text -> Element
22−text : Text -> Element
23
47
48+-- Display centered, styled text.
49+centered : Text -> Element
50+
51+-- Display right justified, styled text.
52+righted : Text -> Element
53+
54+-- Display styled text.
55+text : Text -> Element
56+
57+-- Convert anything to it's textual representation and make it displayable in browser
58+-- asText = text . monospace . show
59+-- Excellent for debugging
24asText : a -> Element
60asText : a -> Element
libraries/JavaScript.elm+22
@@ -5,17 +5,39 @@ module JavaScript where
5-- To Elm
5-- To Elm
6
6
7toString : JSString -> String
7toString : JSString -> String
8+
9+-- Requires that the input array be uniform (all members have the same type)
8toList : JSArray a -> [a]
10toList : JSArray a -> [a]
11+
9toInt : JSNumber -> Int
12toInt : JSNumber -> Int
10toFloat : JSNumber -> Float
13toFloat : JSNumber -> Float
14+
15+-- Conversion from JavaScript boolean values to Elm boolean values.
11toBool : JSBool -> Bool
16toBool : JSBool -> Bool
12
17
13
18
14-- From Elm
19-- From Elm
15
20
16fromString : String -> JSString
21fromString : String -> JSString
22+
23+-- Produces a uniform JavaScript array with all members of the same type.
17fromList : [a] -> JSArray a
24fromList : [a] -> JSArray a
25+
18fromInt : Int -> JSNumber
26fromInt : Int -> JSNumber
19fromFloat : Float -> JSNumber
27fromFloat : Float -> JSNumber
28+
29+-- Conversion from Elm boolean values to JavaScript boolean values.
20fromBool : Bool -> JSBool
30fromBool : Bool -> JSBool
21
31
32+{-- TODO: only found in docs
33+
34+ , ("castTupleToJSTuple2" , "(a,b) -> JSTuple2 a b", "A JSTupleN is an array of size N with nonuniform types. Each member can have a different type.")
35+ , ("castJSTupleToTuple2" , "JSTuple2 a b -> (a,b)", "")
36+ , ("castTupleToJSTuple3" , "(a,b,c) -> JSTuple3 a b c", "")
37+ , ("castJSTupleToTuple3" , "JSTuple3 a b c > (a,b,c)", "")
38+ , ("castTupleToJSTuple4" , "(a,b,c,d) -> JSTuple4 a b c d", "")
39+ , ("castJSTupleToTuple4" , "JSTuple4 a b c d -> (a,b,c,d)", "")
40+ , ("castTupleToJSTuple5" , "(a,b,c,d,e) -> JSTuple5 a b c d e", "")
41+ , ("castJSTupleToTuple5" , "JSTuple5 a b c d e -> (a,b,c,d,e)", "")
42+
43+--}
libraries/Json.elm+17−1
@@ -1,11 +1,14 @@
1
1
2+-- TOOD: Evan please review texts
3+
2module Json where
4module Json where
3
5
4import Dict as Dict
6import Dict as Dict
5import JavaScript as JS
7import JavaScript as JS
6import Native.Json as Native
8import Native.Json as Native
7
9
8−
10+-- This datatype can represent all valid values that can be held in a JSON object.
11+-- In Elm, a proper JSON object is represented as a (Dict String Value) which is a mapping from strings to Json Values.
9data Value
12data Value
10 = String String
13 = String String
11 | Number Float
14 | Number Float
@@ -17,18 +20,25 @@ data Value
17
20
18-- String Converters
21-- String Converters
19
22
23+-- Convert a proper JSON object into a string.
20toString : Value -> String
24toString : Value -> String
21toString v = JS.toString (Native.toPrettyJSString "" v)
25toString v = JS.toString (Native.toPrettyJSString "" v)
22
26
27+-- Convert a proper JSON object into a prettified string.
28+-- The first argument is a separator token (e.g. \" \", \"\\n\", etc.) that will be used for indentation in the prettified string version of the JSON object.
23toPrettyString : String -> Value -> String
29toPrettyString : String -> Value -> String
24toPrettyString sep v = JS.toString (Native.toPrettyJSString sep v)
30toPrettyString sep v = JS.toString (Native.toPrettyJSString sep v)
25
31
32+-- Convert a proper JSON object into a JavaScript string.
33+-- Note that the type JSString seen here is not the same as the type constructor JsonString used elsewhere in this module.
26toJSString : Value -> JSString
34toJSString : Value -> JSString
27toJSString v = Native.toPrettyJSString "" v
35toJSString v = Native.toPrettyJSString "" v
28
36
37+-- Parse a string representation of a proper JSON object into its Elm's representation.
29fromString : String -> Maybe Value
38fromString : String -> Maybe Value
30fromString s = Native.fromJSString (JS.fromString s)
39fromString s = Native.fromJSString (JS.fromString s)
31
40
41+-- Parse a JavaScript string representation of a proper JSON object into its Elm's representation.
32fromJSString : JSString -> Maybe Value
42fromJSString : JSString -> Maybe Value
33
43
34
44
@@ -52,6 +62,7 @@ object v = case v of { Object o -> o ; _ -> Dict.empty }
52
62
53-- Extract Elm values from dictionaries of Json values
63-- Extract Elm values from dictionaries of Json values
54
64
65+-- Find a value in a Json Object using the passed get function. If the key is not found, this returns the given default/base value.
55find get base =
66find get base =
56 let f key dict =
67 let f key dict =
57 case Dict.lookup key dict of
68 case Dict.lookup key dict of
@@ -59,17 +70,22 @@ find get base =
59 Just v -> get v
70 Just v -> get v
60 in f
71 in f
61
72
73+-- Find a string value in an Elm Json object. If the key is not found or the value found is not a string, this returns the empty string.
62findString : String -> Dict String Value -> String
74findString : String -> Dict String Value -> String
63findString = find string ""
75findString = find string ""
64
76
77+-- Find a number value in an Elm Json object. If the key is not found or the value found is not a number, this returns 0
65findNumber : String -> Dict String Value -> Float
78findNumber : String -> Dict String Value -> Float
66findNumber = find number 0
79findNumber = find number 0
67
80
81+-- Find a boolean value in an Elm Json object. If the key is not found or the value found is not a boolean, this returns the False.
68findBoolean : String -> Dict String Value -> Bool
82findBoolean : String -> Dict String Value -> Bool
69findBoolean = find boolean False
83findBoolean = find boolean False
70
84
85+-- Find an array value in an Elm Json object. If the key is not found or the value found is not an array, this returns an empty list.
71findArray : String -> Dict String Value -> [Value]
86findArray : String -> Dict String Value -> [Value]
72findArray = find array []
87findArray = find array []
73
88
89+-- Find an object value in an Elm Json object. If the key is not found or the value found is not an object, this returns an empty object.
74findObject : String -> Dict String Value -> Dict String Value
90findObject : String -> Dict String Value -> Dict String Value
75findObject = find object Dict.empty
91findObject = find object Dict.empty
libraries/Keyboard.elm−1
@@ -1,7 +1,6 @@
1-- These are nicely curated inputs from the keyboard. See the
1-- These are nicely curated inputs from the keyboard. See the
2-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
2-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
3-- lower-level interface that will let you define more complicated behavior.
3-- lower-level interface that will let you define more complicated behavior.
4−
5module Keyboard where
4module Keyboard where
6
5
7import Native.Keyboard as N
6import Native.Keyboard as N
libraries/List.elm+71−1
@@ -4,36 +4,77 @@ module List where
4import Native.Utils (min, max)
4import Native.Utils (min, max)
5import Native.List as L
5import Native.List as L
6
6
7+-- Extract the first element of a list. List must be non-empty.
7head : [a] -> a
8head : [a] -> a
9+
10+-- Extract the elements after the head of the list. List must be non-empty.
8tail : [a] -> [a]
11tail : [a] -> [a]
12+
13+-- Extract the last element of a list. List must be non-empty.
9last : [a] -> a
14last : [a] -> a
15+
16+-- Apply a function to every element of a list.
10map : (a -> b) -> [a] -> [b]
17map : (a -> b) -> [a] -> [b]
18+
19+-- Reduce a list from the left.
11foldl : (a -> b -> b) -> b -> [a] -> b
20foldl : (a -> b -> b) -> b -> [a] -> b
21+
22+-- Reduce a list from the right.
12foldr : (a -> b -> b) -> b -> [a] -> b
23foldr : (a -> b -> b) -> b -> [a] -> b
24+
25+-- Reduce a list from the left without a base case. List must be non-empty.
13foldl1 : (a -> a -> a) -> [a] -> a
26foldl1 : (a -> a -> a) -> [a] -> a
27+
28+-- Reduce a list from the right without a base case. List must be non-empty.
14foldr1 : (a -> a -> a) -> [a] -> a
29foldr1 : (a -> a -> a) -> [a] -> a
15
30
31+-- Reduce a list from the left, building up all of the intermediate results into a list.
16scanl : (a -> b -> b) -> b -> [a] -> [b]
32scanl : (a -> b -> b) -> b -> [a] -> [b]
33+
34+-- Same as scanl but it doesn't require a base case. List must be non-empty.
17scanl1 : (a -> a -> a) -> [a] -> [a]
35scanl1 : (a -> a -> a) -> [a] -> [a]
18
36
37+-- Filter out elements which do not satisfy the predicate.
19filter : (a -> Bool) -> [a] -> [a]
38filter : (a -> Bool) -> [a] -> [a]
39+
40+-- Determine the length of a list.
20length : [a] -> Int
41length : [a] -> Int
42+
43+-- Reverse a list.
21reverse : [a] -> [a]
44reverse : [a] -> [a]
22
45
46+-- Check to see if all elements satisfy the predicate.
23all : (a -> Bool) -> [a] -> Bool
47all : (a -> Bool) -> [a] -> Bool
48+
49+-- Check to see if any elements satisfy the predicate.
24any : (a -> Bool) -> [a] -> Bool
50any : (a -> Bool) -> [a] -> Bool
51+
52+-- Check to see if all elements are True.
25and : [Bool] -> Bool
53and : [Bool] -> Bool
54+
55+-- Check to see if any elements are True.
26or : [Bool] -> Bool
56or : [Bool] -> Bool
27
57
58+-- Flatten a list of lists.
28concat : [[a]] -> [a]
59concat : [[a]] -> [a]
60+
61+-- Map a given function onto a list and flatten the resulting lists. (concatMap f xs == concat (map f xs))
29concatMap : (a -> [b]) -> [a] -> [b]
62concatMap : (a -> [b]) -> [a] -> [b]
30concatMap f = L.concat . L.map f
63concatMap f = L.concat . L.map f
31
64
65+-- Get the sum of the list elements.
32sum = L.foldl (+) 0
66sum = L.foldl (+) 0
67+
68+-- Get the product of the list elements.
33product = L.foldl (*) 1
69product = L.foldl (*) 1
70+
71+-- Find the highest number in a non-empty list.
34maximum = L.foldl1 max
72maximum = L.foldl1 max
73+
74+-- Find the lowest number in a non-empty list.
35minimum = L.foldl1 min
75minimum = L.foldl1 min
36
76
77+-- Split a list based on the predicate.
37partition : (a -> Bool) -> [a] -> ([a],[a])
78partition : (a -> Bool) -> [a] -> ([a],[a])
38partition pred lst =
79partition pred lst =
39 case lst of
80 case lst of
@@ -41,18 +82,28 @@ partition pred lst =
41 x::xs -> let (bs,cs) = partition pred xs in
82 x::xs -> let (bs,cs) = partition pred xs in
42 if pred x then (x::bs,cs) else (bs,x::cs)
83 if pred x then (x::bs,cs) else (bs,x::cs)
43
84
44−zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
85+-- Combine two lists, combining them into tuples pairwise. If one input list has extra elements (it is longer), those elements are dropped.
45zip : [a] -> [b] -> [(a,b)]
86zip : [a] -> [b] -> [(a,b)]
46
87
88+-- Combine two lists, combining them with the given function. If one input list has extra elements (it is longer), those elements are dropped.
89+zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
90+
91+-- Decompose a list of tuples
47unzip : [(a,b)] -> ([a],[b])
92unzip : [(a,b)] -> ([a],[b])
48unzip pairs =
93unzip pairs =
49 case pairs of
94 case pairs of
50 [] -> ([],[])
95 [] -> ([],[])
51 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
96 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
52
97
98+-- Split a list
99+-- split "," "hello,there,friend" == ["hello", "there", "friend"]
53split : [a] -> [a] -> [[a]]
100split : [a] -> [a] -> [[a]]
101+
102+-- Places the given value between all of the lists in the second argument and concatenates the result.
103+-- join xs xss = concat (intersperse xs xss)
54join : [a] -> [[a]] -> [a]
104join : [a] -> [[a]] -> [a]
55
105
106+-- Places the given value between all members of the given list.
56intersperse : a -> [a] -> [a]
107intersperse : a -> [a] -> [a]
57intersperse sep xs =
108intersperse sep xs =
58 case xs of
109 case xs of
@@ -60,3 +111,22 @@ intersperse sep xs =
60 [a] -> [a]
111 [a] -> [a]
61 [] -> []
112 [] -> []
62
113
114+{-- TODO: only found in docs
115+
116+-- Add an element to the front of a list
117+-- a :: [b,c] = [a,b,c]
118+(::) : a -> [a] -> [a]
119+
120+-- Appends two lists.
121+(++) : [a] -> [a] -> [a]
122+
123+-- Take the first n members of a list.
124+-- take 2 [1,2,3,4]) ==> [1,2]
125+take : Int -> [a] -> [a]
126+
127+-- Drop the first n members of a list.
128+-- drop 2 [1,2,3,4]) ==> [3,4]
129+drop : Int -> [a] -> [a]
130+
131+--}
132+
libraries/Set.elm+14−1
@@ -11,41 +11,54 @@ import List as List
11
11
12type Set t = Dict t ()
12type Set t = Dict t ()
13
13
14+-- Create an empty set.
14empty : Set t
15empty : Set t
15empty = Dict.empty
16empty = Dict.empty
16
17
18+-- Create a set with one value.
17singleton : t -> Set t
19singleton : t -> Set t
18singleton k = Dict.singleton k ()
20singleton k = Dict.singleton k ()
19
21
22+-- Insert a value into a set.
20insert : t -> Set t -> Set t
23insert : t -> Set t -> Set t
21insert k = Dict.insert k ()
24insert k = Dict.insert k ()
22
25
26+-- Remove a value from a set. If the value is not found, no changes are made.
23remove : t -> Set t -> Set t
27remove : t -> Set t -> Set t
24remove = Dict.remove
28remove = Dict.remove
25
29
30+-- Determine if a value is in a set.
26member : t -> Set t -> Bool
31member : t -> Set t -> Bool
27member = Dict.member
32member = Dict.member
28
33
34+-- Get the union of two sets. Keep all values.
29union : Set t -> Set t -> Set t
35union : Set t -> Set t -> Set t
30union = Dict.union
36union = Dict.union
31
37
38+-- Get the intersection of two sets. Keeps values that appear in both sets.
32intersect : Set t -> Set t -> Set t
39intersect : Set t -> Set t -> Set t
33intersect = Dict.intersect
40intersect = Dict.intersect
34
41
42+-- Get the difference between the first set and the second. Keeps values that do not appear in the second set.
35diff : Set t -> Set t -> Set t
43diff : Set t -> Set t -> Set t
36diff = Dict.diff
44diff = Dict.diff
37
45
46+-- Convert a set into a list.
38toList : Set t -> [t]
47toList : Set t -> [t]
39toList = Dict.keys
48toList = Dict.keys
40
49
50+-- Convert a list into a set, removing any duplicates.
41fromList : [t] -> Set t
51fromList : [t] -> Set t
42fromList xs = List.foldl (\k t -> Dict.insert k () t) empty xs
52fromList xs = List.foldl (\k t -> Dict.insert k () t) empty xs
43
53
54+-- Fold over the values in a set, in order from lowest to highest.
44foldl : (a -> b -> b) -> b -> Set a -> b
55foldl : (a -> b -> b) -> b -> Set a -> b
45foldl f b s = Dict.foldl (\k _ b -> f k b) b s
56foldl f b s = Dict.foldl (\k _ b -> f k b) b s
46
57
58+-- Fold over the values in a set, in order from highest to lowest.
47foldr : (a -> b -> b) -> b -> Set a -> b
59foldr : (a -> b -> b) -> b -> Set a -> b
48foldr f b s = Dict.foldr (\k _ b -> f k b) b s
60foldr f b s = Dict.foldr (\k _ b -> f k b) b s
49
61
62+-- Map a function onto a set, creating a new set with no duplicates.
50map : (a -> b) -> Set a -> Set b
63map : (a -> b) -> Set a -> Set b
51−map f s = fromList (List.map f (toList s))
64+map f s = fromList (List.map f (toList s))
libraries/Signal.elm+90−1
@@ -1,12 +1,24 @@
1
1
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`.
2module Signal where
8module Signal where
3
9
4import Native.Signal as Native
10import Native.Signal as Native
5
11
12+-- Create a constant signal that never changes.
6constant : a -> Signal a
13constant : a -> Signal a
7
14
15+-- Transform a signal with a given function.
8lift : (a -> b) -> Signal a -> Signal b
16lift : (a -> b) -> Signal a -> Signal b
17+
18+-- Combine two signals with a given function.
9lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
19lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
20+
21+-- Combine three signals with a given function.
10lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
22lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
11lift4 : (a -> b -> c -> d -> e) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e
23lift4 : (a -> b -> c -> d -> e) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e
12lift5 : (a -> b -> c -> d -> e -> f) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f
24lift5 : (a -> b -> c -> d -> e -> f) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f
@@ -14,21 +26,98 @@ lift6 : (a -> b -> c -> d -> e -> f -> g) -> Signal a -> Signal b -> Signal c ->
14lift7 : (a -> b -> c -> d -> e -> f -> g -> h) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h
26lift7 : (a -> b -> c -> d -> e -> f -> g -> h) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h
15lift8 : (a -> b -> c -> d -> e -> f -> g -> h -> i) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h -> Signal i
27lift8 : (a -> b -> c -> d -> e -> f -> g -> h -> i) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h -> Signal i
16
28
29+-- Create a past-dependent signal. Each value given on the input signal will
30+-- be accumulated, producing a new output value.
31+--
32+-- For instance, `(foldp (\\t acc -> acc + 1) 0 (Time.every second))` increments every second.
17foldp : (a -> b -> b) -> b -> Signal a -> Signal b
33foldp : (a -> b -> b) -> b -> Signal a -> Signal b
18
34
35+-- Merge two signals into one, biased towards the first signal if both signals
36+-- update at the same time.
19merge : Signal a -> Signal a -> Signal a
37merge : Signal a -> Signal a -> Signal a
38+
39+-- Merge many signals into one, biased towards the left-most signal if multiple
40+-- signals update simultaneously.
20merges : [Signal a] -> Signal a
41merges : [Signal a] -> Signal a
42+
43+-- Merge two signals into one, but distinguishing the values by marking the first
44+-- signal as `Left` and the second signal as `Right`. This allows you to easily
45+-- fold over non-homogeneous inputs.
21mergeEither : Signal a -> Signal b -> Signal (Either a b)
46mergeEither : Signal a -> Signal b -> Signal (Either a b)
22
47
48+-- Count the number of events that have occured.
23count : Signal a -> Signal Int
49count : Signal a -> Signal Int
50+
51+-- Count the number of events that have occured that satisfy a given predicate.
24countIf : (a -> Bool) -> Signal a -> Signal Int
52countIf : (a -> Bool) -> Signal a -> Signal Int
25
53
54+-- Keep only events that satisfy the given predicate. Elm does not allow
55+-- undefined signals, so a base case must be provided in case the predicate is
56+-- never satisfied.
26keepIf : (a -> Bool) -> a -> Signal a -> Signal a
57keepIf : (a -> Bool) -> a -> Signal a -> Signal a
58+
59+-- Drop events that satisfy the given predicate. Elm does not allow undefined
60+-- signals, so a base case must be provided in case the predicate is never
61+-- satisfied.
27dropIf : (a -> Bool) -> a -> Signal a -> Signal a
62dropIf : (a -> Bool) -> a -> Signal a -> Signal a
63+
64+-- Keep events only when the first signal is true. When the first signal becomes
65+-- true, the most recent value of the second signal will be propagated. Until
66+-- the first signal becomes false again, all events will be propagated. Elm does
67+-- not allow undefined signals, so a base case must be provided in case the first
68+-- signal is never true.
28keepWhen : Signal Bool -> a -> Signal a -> Signal a
69keepWhen : Signal Bool -> a -> Signal a -> Signal a
70+
71+-- Drop events when the first signal is true. When the first signal becomes false,
72+-- the most recent value of the second signal will be propagated. Until the first
73+-- signal becomes true again, all events will be propagated. Elm does not allow
74+-- undefined signals, so a base case must be provided in case the first signal is
75+-- always true.
29dropWhen : Signal Bool -> a -> Signal a -> Signal a
76dropWhen : Signal Bool -> a -> Signal a -> Signal a
77+
78+-- Drop sequential repeated values. For example, if a signal produces the
79+-- sequence `[1,1,2,2,1]`, it becomes `[1,2,1]` by dropping the values that
80+-- are the same as the previous value.
30dropRepeats : Signal a -> Signal a
81dropRepeats : Signal a -> Signal a
31
82
83+-- Sample from the second input every time an event occurs on the first input.
84+-- For example, `(sampleOn clicks (every second))` will give the approximate
85+-- time of the latest click.
32sampleOn : Signal a -> Signal b -> Signal b
86sampleOn : Signal a -> Signal b -> Signal b
33
87
88+-- Add a timestamp to any signal. Timestamps increase monotonically. Each timestamp is
89+-- related to a specfic event, so `Mouse.x` and `Mouse.y` will always have the same
90+-- timestamp because they both rely on the same underlying event.
34−timestamp : Signal a -> Signal (Time, a)
91+timestamp : Signal a -> Signal (Time, a)
92+
93+-- Delay a signal by a certain amount of time. So `(delay second Mouse.clicks)`
94+-- will update one second later than any mouse click.
95+delay : Time -> Signal a -> Signal a
96+
97+{-- TODO: only found in docs
98+ , ("average", "Int -> Signal Number -> Signal Float", [markdown|
99+
100+Takes an integer `n` and a signal of numbers. Computes the running
101+average of the signal over the last `n` events.
102+
103+So `(average 20 (fps 40))` would be the average time between the frames for
104+the last 20 frames.|])
105+ , ("foldp1", "(a -> a -> a) -> Signal a -> Signal a", [markdown|
106+Create a past-dependent signal. The first value on the signal is used
107+as the base case.|])
108+ , ("foldp'", "(a -> b -> b) -> (a -> b) -> Signal a -> Signal b", [markdown|
109+Just like foldp, but instead of a base case, you provide a function to be
110+applied to the first value, creating the base case.|])
111+ [ ("(<~)", "(a -> b) -> Signal a -> Signal b", [markdown|
112+An alias for `lift`. A prettier way to apply a
113+function to the current value of a signal.|])
114+ , ("(~)", "Signal (a -> b) -> Signal a -> Signal b", [markdown|
115+Signal application. This takes two signals, holding a function and
116+a value. It applies the current function to the current value.
117+
118+So the following expressions are equivalent:
119+
120+ scene <~ Mouse.x ~ Mouse.y
121+ lift2 scene Mouse.x Mouse.y|])
122+--}
123+
libraries/Time.elm+13−3
@@ -1,6 +1,4 @@
1−-- Library for working with time. Type `Time` represents some number of
1+-- Library for working with time. Type `Time` represents some number of milliseconds.
2−-- milliseconds.
3−
4module Time where
2module Time where
5
3
6import Native.Time as T
4import Native.Time as T
@@ -50,3 +48,15 @@ fpsWhen : Number -> Signal Bool -> Signal Time
50-- Takes a time interval t. The resulting signal is the current time,
48-- Takes a time interval t. The resulting signal is the current time,
51-- updated every t.
49-- updated every t.
52every : Time -> Signal Time
50every : Time -> Signal Time
51+
52+-- Takes a time `t` and any signal. The resulting boolean signal
53+-- is true for time `t` after every event on the input signal.
54+-- So ``(second `since` Mouse.clicks)`` would result in a signal
55+-- that is true for one second after each mouse click and false
56+-- otherwise.
57+since : Time -> Signal a -> Signal Bool
58+
59+{-- TODO: Only found in docs
60+ , ("timeOf", "Signal a -> Signal Time", [markdown|
61+ Same as `timestamp` but it throws out the incoming value. So `(timeOf == lift fst . timestamp)`.|])
62+--}
libraries/Touch.elm−1
@@ -1,6 +1,5 @@
1-- This is an early version of the touch library. It will likely grow to
1-- This is an early version of the touch library. It will likely grow to
2-- include gestures that would be useful for both games and web-pages.
2-- include gestures that would be useful for both games and web-pages.
3−
4module Touch where
3module Touch where
5
4
6import Native.Touch as T
5import Native.Touch as T
libraries/WebSocket.elm−1
@@ -1,6 +1,5 @@
1-- A library for low latency HTTP communication. See the HTTP library for
1-- A library for low latency HTTP communication. See the HTTP library for
2-- standard requests like GET, POST, etc.
2-- standard requests like GET, POST, etc.
3−
4module WebSocket where
3module WebSocket where
5
4
6import Native.WebSocket as WS
5import Native.WebSocket as WS