on master help sign in
Add lots of documentation and do some minor API clean up.
commitc92dd2f9214912b1ae77758f7612e297997a10d7 authorevancz <info@elm-lang.org> dateFri 17 May 2013, 22:46 (13 years ago) parent17a44f3
20 files changed+310−132
libraries/Automaton.elm+1 libraries/Color.elm+7−17 libraries/Graphics/Collage.elm+67−13 libraries/Graphics/Element.elm+49−3 libraries/JavaScript.elm+7−20 libraries/JavaScript/Experimental.elm+21 libraries/Keyboard.elm+6−6 libraries/List.elm+40−22 libraries/Matrix2D.elm+29−11 libraries/Mouse.elm−2 libraries/Native/Color.js+13−7 libraries/Native/Graphics/Input.js−1 libraries/Native/JavaScript.js+24−3 libraries/Native/Matrix2D.js+12−4 libraries/Native/Prelude.js+1−6 libraries/Native/Utils.js+8 libraries/Signal.elm+4−2 libraries/Text.elm+5−3 libraries/Time.elm+8−6 libraries/Touch.elm+8−6
libraries/Automaton.elm+1
@@ -43,6 +43,7 @@ pure f = Step (\x -> (pure f, f x))
4343-- function to step the state forward. For example, an automaton that counted
4444-- how many steps it has taken would look like this:
4545--
46+-- count = Automaton a Int
4647-- count = state 0 (\\_ c -> c+1)
4748--
4849-- It is a stateful automaton. The initial state is zero, and the step function
libraries/Color.elm+7−17
@@ -57,25 +57,15 @@ forestGreen = Color 34 139 34 1
5757-- The two colors will accent each other.
5858complement : Color -> Color
5959
60−-- Create HSV colors with an alpha component for transparency.
61−-- The alpha component is specified with numbers between 0 and 1.
62−hsva : Int -> Float -> Float -> Float -> Color
60+-- Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV)
61+-- with an alpha component for transparency.
62+hsva : Float -> Float -> Float -> Float -> Color
6363
64−-- Create HSV colors. HSV stands for hue-saturation-value.
65−--
66−-- Hue is a degree from 0 to 360 representing a color wheel: red at 0&deg;,
67−-- green at 120&deg;, blue at 240&deg;, and red again at 360&deg;.
68−-- This makes it easy to cycle through colors and compute color complements,
69−-- triads, tetrads, etc.
70−--
71−-- Saturation is a number between 1 and 0 where lowering this number makes
72−-- your color more grey. This can help you tone a color down.
73−--
74−-- Value is also a number between 1 and 0. Lowering this number makes your
75−-- color more black.
64+-- Create [HSV colors](http://en.wikipedia.org/wiki/HSL_and_HSV).
65+-- This is very convenient for creating colors that cycle and shift.
7666--
77−-- Look up the &ldquo;HSV cylinder&rdquo; for more information.
78−hsv : Int -> Float -> Float -> Color
67+-- hsv (degrees 240) 1 1 == blue
68+hsv : Float -> Float -> Float -> Color
7969
8070data Gradient
8171 = Linear (Float,Float) (Float,Float) [(Float,Color)]
libraries/Graphics/Collage.elm+67−13
@@ -22,35 +22,49 @@ data FillStyle
2222 | Texture String
2323 | Gradient Gradient
2424
25−data LineCap = Butt | Round | Square
26−data LineJoin = Smooth | Sharp | Clipped
25+-- The shape of the ends of a line.
26+data LineCap = Flat | Round | Padded
2727
28+-- The shape of the &ldquo;joints&rdquo; of a line, where each line segment
29+-- meets. `Sharp` takes an argument to limit the length of the joint. This
30+-- defaults to 10.
31+data LineJoin = Smooth | Sharp Float | Clipped
32+
33+-- All of the attributes of a line style. This lets you build up a line style
34+-- however you want. You can also update existing line styles with record updates.
2835type LineStyle = {
2936 color : Color,
3037 width : Float,
3138 cap : LineCap,
3239 join : LineJoin,
33− miterLimit : Float,
3440 dashing : [Int],
3541 dashOffset : Int
3642 }
3743
44+-- The default line style, which is solid black with flat caps and sharp joints.
45+-- You can use record updates to build the line style you
46+-- want. For example, to make a thicker line, you could say:
47+--
48+-- { defaultLine | width <- 10 }
3849defaultLine : LineStyle
3950defaultLine = {
4051 color = Color.black,
4152 width = 1,
42− cap = Butt,
43− join = Sharp,
53+ cap = Flat,
54+ join = Sharp 10,
4455 dashing = [],
4556 dashOffset = 0,
46− miterLimit = 10
4757 }
4858
49−-- default LineStyles
59+-- Create a solid line style with a given color.
5060solid : Color -> LineStyle
5161solid clr = { defaultLine | color <- clr }
62+
63+-- Create a dashed line style with a given color. Dashing equals `[8,4]`.
5264dashed : Color -> LineStyle
5365dashed clr = { defaultLine | color <- clr, dashing <- [8,4] }
66+
67+-- Create a dotted line style with a given color. Dashing equals `[3,3]`.
5468dotted : Color -> LineStyle
5569dotted clr = { defaultLine | color <- clr, dashing <- [3,3] }
5670
@@ -65,72 +79,106 @@ form f = { theta = 0, scale = 1, x = 0, y = 0, form = f }
6579
6680fill style shape = form (FShape (Right style) shape)
6781
82+-- Create a filled in shape.
6883filled : Color -> Shape -> Form
6984filled color shape = fill (Solid color) shape
7085
86+-- Create a textured shape. The texture is described by some url and is
87+-- tiled to fill the entire shape.
7188textured : String -> Shape -> Form
7289textured src shape = fill (Texture src) shape
7390
91+-- Fill a shape with a [gradient](/docs/Color.elm#linear).
7492gradient : Gradient -> Shape -> Form
7593gradient grad shape = fill (Gradient grad) shape
7694
95+-- Outline a shape with a given line style.
7796outlined : LineStyle -> Shape -> Form
7897outlined style shape = form (FShape (Left style) shape)
7998
99+-- Trace a path with a given line style.
80100traced : LineStyle -> Path -> Form
81101traced style path = form (FPath style path)
82102
83−-- Draw a sprite
84−-- w width
85−-- h height
86−-- pos (px,py) coordinates into the sprite map
87−-- src The location of the image
103+-- Create a sprite from a sprite sheet. It cuts out a rectangle
104+-- at a given position.
88105sprite : Int -> Int -> (Int,Int) -> String -> Form
89106sprite w h pos src = form (FImage w h pos src)
90107
108+-- Turn any `Element` into a `Form`. This lets you use text, gifs, and video
109+-- in your collage. This means you can move, rotate, and scale
110+-- an `Element` however you want.
91111toForm : Element -> Form
92112toForm e = form (FElement e)
93113
114+-- Flatten many forms into a single `Form`. This lets you move and rotate them
115+-- as a single unit, making it possible to build small, modular components.
94116group : [Form] -> Form
95117group fs = form (FGroup Matrix.identity fs)
96118
97−groupTransform : Matrix -> [Form] -> Form
119+-- Flatten many forms into a single `Form` and then apply a matrix
120+-- transformation.
121+groupTransform : Matrix2D -> [Form] -> Form
98122groupTransform matrix fs = form (FGroup matrix fs)
99123
124+-- Rotate a form by a given angle. Rotate takes standard Elm angles (radians)
125+-- and turns things counterclockwise. So to turn `form` 30&deg; to the left
126+-- you would say, `(rotate (degrees 30) form)`.
100127rotate : Float -> Form -> Form
101128rotate t f = { f | theta <- f.theta + t }
102129
130+-- Scale a form by a given factor. Scaling by 2 doubles the size.
103131scale : Float -> Form -> Form
104132scale s f = { f | scale <- f.scale * s }
105133
134+-- Move a form by the given amount. This is a relative translation so
135+-- `(move (10,10) form)` would move `form` ten pixels up and ten pixels to the
136+-- right.
106137move : (Float,Float) -> Form -> Form
107138move (x,y) f = { f | x <- f.x + x, y <- f.y + y }
108139
140+-- Move a shape in the x direction. This is relative so `(moveX 10 form)` moves
141+-- `form` 10 pixels to the right.
109142moveX : Float -> Form -> Form
110143moveX x f = { f | x <- f.x + x }
111144
145+-- Move a shape in the y direction. This is relative so `(moveY 10 form)` moves
146+-- `form` upwards by 10 pixels.
112147moveY : Float -> Form -> Form
113148moveY y f = { f | y <- f.y + y }
114149
150+-- A collage is a collection of 2D forms. There are no strict positioning
151+-- relationships between forms, so you are free to do all kinds of 2D graphics.
115152collage : Int -> Int -> [Form] -> Element
116153
117154
118155type Path = [(Float,Float)]
119156
157+-- Create a path that follows a sequence of points.
120158path : [(Number a,Number a)] -> Path
121159path ps = ps
122160
161+-- Create a path along a given line segment.
123162segment : (Number a,Number a) -> (Number a,Number a) -> Path
124163segment p1 p2 = [p1,p2]
125164
126165type Shape = [(Float,Float)]
127166
167+-- Create an arbitrary polygon by specifying its corners in order.
168+-- `polygon` will automatically close all shapes, so the given list
169+-- of points does not need to start and end with the same position.
128170polygon : [(Number a,Number a)] -> Shape
129171polygon points = points
130172
173+-- A rectangle with a given width and height.
131174rect : Number a -> Number a -> Shape
132175rect w h = [ (0-w/2,0-h/2), (0-w/2,h/2), (w/2,h/2), (w/2,0-h/2) ]
133176
177+-- A square with a given edge length.
178+square : Number a -> Shape
179+square n = rect w h
180+
181+-- An oval with a given width and height.
134182oval : Number a -> Number a -> Shape
135183oval w h =
136184 let n = 50
@@ -140,9 +188,15 @@ oval w h =
140188 f i = (hw * Math.cos (t*i), hh * Math.sin (t*i))
141189 in map f [0..n-1]
142190
191+-- A circle with a given radius.
143192circle : Number a -> Shape
144193circle r = oval (2*r) (2*r)
145194
195+-- A regular polygon with N sides. The first argument specifies the number
196+-- of sides and the second is the radius. So to create a pentagon with radius
197+-- 30 you would say:
198+--
199+-- ngon 5 30
146200ngon : Int -> Number a -> Shape
147201ngon n r =
148202 let m = toFloat n
libraries/Graphics/Element.elm+49−3
@@ -30,15 +30,19 @@ type Properties = {
3030
3131type Element = { props : Properties, element : ElementPrim }
3232
33+-- Get the width of an Element
3334widthOf : Element -> Int
34−widthOf e = e.props.width
35+widthOf e = e.props.width
3536
37+-- Get the height of an Element
3638heightOf : Element -> Int
3739heightOf e = e.props.height
3840
39−sizeOf : Element -> Int
40−sizeOf e = (e.props.width, e.props.height)
41+-- Get the width and height of an Element
42+sizeOf : Element -> (Int,Int)
43+sizeOf e = (e.props.width, e.props.height)
4144
45+-- Create an `Element` with a given width.
4246width : Int -> Element -> Element
4347width nw e = let p = e.props
4448 props = case e.element of
@@ -47,6 +51,7 @@ width nw e = let p = e.props
4751 _ -> p
4852 in { element=e.element, props={props| width <- nw} }
4953
54+-- Create an `Element` with a given height.
5055height : Int -> Element -> Element
5156height nh e = let p = e.props
5257 props = case e.element of
@@ -54,17 +59,24 @@ height nh e = let p = e.props
5459 _ -> p
5560 in { element=e.element, props={p| height <- nh} }
5661
62+-- Create an `Element` with a given opacity. Opacity is a number between 0 and 1
63+-- where 0 means totally clear.
5764opacity : Float -> Element -> Element
5865opacity o e = let p = e.props in { element=e.element, props={p| opacity <- o} }
5966
67+-- Create an `Element` with a given background color.
6068color : Color -> Element -> Element
6169color c e = let p = e.props in
6270 { element=e.element, props={p| color <- Just c} }
6371
72+-- Create an `Element` with a tag. This lets you link directly to it.
73+-- The element `(tag "all-about-badgers" thirdParagraph)` can be reached
74+-- with a link lik this: `/facts-about-animals.elm#all-about-badgers`
6475tag : String -> Element -> Element
6576tag name e = let p = e.props in
6677 { element=e.element, props={p| tag <- JS.fromString name} }
6778
79+-- Create an `Element` that is a hyper-link.
6880link : String -> Element -> Element
6981link href e = let p = e.props in
7082 { element=e.element, props={p| href <- JS.fromString href} }
@@ -83,12 +95,20 @@ data ElementPrim
8395
8496data ImageStyle = Plain | Fitted | Cropped (Int,Int)
8597
98+-- Create an image given a width, height, and image source.
8699image : Int -> Int -> String -> Element
87100image w h src = newElement w h (Image Plain w h (JS.fromString src))
88101
102+-- Create a fitted image given a width, height, and image source.
103+-- This will crop the picture to best fill the given dimensions.
89104fittedImage : Int -> Int -> String -> Element
90105fittedImage w h src = newElement w h (Image Fitted w h (JS.fromString src))
91106
107+-- Create a cropped image. Take a rectangle out of the picture starting
108+-- at the given top left coordinate. If you have a 140-by-140 image,
109+-- the following will cut a 100-by-100 square out of the middle of it.
110+--
111+-- croppedImage 100 100 (20,20) "yogi.jpg"
92112croppedImage : Int -> Int -> (Int,Int) -> String -> Element
93113croppedImage w h pos src =
94114 newElement w h (Image (Cropped pos) w h (JS.fromString src))
@@ -97,14 +117,31 @@ data Three = P | Z | N
97117data Pos = Absolute Int | Relative Float
98118type Position = { horizontal : Three, vertical : Three, x : Pos, y : Pos }
99119
120+-- Put an element in a container. This lets you position the element really
121+-- easily, and there are tons of ways to set the `Position`.
122+-- To center `element` exactly in a 300-by-300 square you would say:
123+--
124+-- container 300 300 middle element
125+--
126+-- By setting the color of the container, you can create borders.
100127container : Int -> Int -> Position -> Element -> Element
101128container w h pos e = newElement w h (Container pos e)
102129
130+-- Create an empty box. This is useful for getting your spacing right and
131+-- for making borders.
103132spacer : Int -> Int -> Element
104133spacer w h = newElement w h Spacer
105134
106135data Direction = DUp | DDown | DLeft | DRight | DIn | DOut
107136
137+-- Have a list of elements flow in a particular direction.
138+-- The `Direction` starts from the first element in the list.
139+--
140+-- flow right [a,b,c]
141+--
142+-- +---+---+---+
143+-- | a | b | c |
144+-- +---+---+---+
108145flow : Direction -> [Element] -> Element
109146flow dir es =
110147 let ws = List.map widthOf es
@@ -120,24 +157,33 @@ flow dir es =
120157 DIn -> newFlow (List.maximum ws) (List.maximum hs)
121158 DOut -> newFlow (List.maximum ws) (List.maximum hs)
122159
160+-- Stack elements vertically. To put `a` above `b` you would say:
161+--
162+-- a `above` b
123163above : Element -> Element -> Element
124164above hi lo =
125165 newElement (max (widthOf hi) (widthOf lo))
126166 (heightOf hi + heightOf lo)
127167 (Flow DDown [hi,lo])
128168
169+-- Stack elements vertically. To put `a` below `b` you would say:
170+--
171+-- a `below` b
129172below : Element -> Element -> Element
130173below lo hi =
131174 newElement (max (widthOf hi) (widthOf lo))
132175 (heightOf hi + heightOf lo)
133176 (Flow DDown [hi,lo])
134177
178+-- Put elements beside each other horizontally.
135179beside : Element -> Element -> Element
136180beside lft rht =
137181 newElement (widthOf lft + widthOf rht)
138182 (max (heightOf lft) (heightOf rht))
139183 (Flow right [lft,rht])
140184
185+-- Layer elements on top of each other, starting from the bottom.
186+-- `(layers == flow outward)`
141187layers : [Element] -> Element
142188layers es =
143189 let ws = List.map widthOf es
libraries/JavaScript.elm+7−20
@@ -2,38 +2,25 @@
22module JavaScript where
33
44
5−-- To Elm
6−
7−toString : JSString -> String
8−
95-- Requires that the input array be uniform (all members have the same type)
106toList : JSArray a -> [a]
11−
127toInt : JSNumber -> Int
138toFloat : JSNumber -> Float
149toBool : JSBool -> Bool
10+toString : JSString -> String
1511
1612
17−-- From Elm
18−
19−fromString : String -> JSString
20−
2113-- Produces a uniform JavaScript array with all members of the same type.
2214fromList : [a] -> JSArray a
23−
2415fromInt : Int -> JSNumber
2516fromFloat : Float -> JSNumber
2617fromBool : Bool -> JSBool
18+fromString : String -> JSString
2719
28−{-- TODO: only found in docs
2920
30− , ("castTupleToJSTuple2" , "(a,b) -> JSTuple2 a b", "A JSTupleN is an array of size N with nonuniform types. Each member can have a different type.")
31− , ("castJSTupleToTuple2" , "JSTuple2 a b -> (a,b)", "")
32− , ("castTupleToJSTuple3" , "(a,b,c) -> JSTuple3 a b c", "")
33− , ("castJSTupleToTuple3" , "JSTuple3 a b c > (a,b,c)", "")
34− , ("castTupleToJSTuple4" , "(a,b,c,d) -> JSTuple4 a b c d", "")
35− , ("castJSTupleToTuple4" , "JSTuple4 a b c d -> (a,b,c,d)", "")
36− , ("castTupleToJSTuple5" , "(a,b,c,d,e) -> JSTuple5 a b c d e", "")
37− , ("castJSTupleToTuple5" , "JSTuple5 a b c d e -> (a,b,c,d,e)", "")
21+-- Turn an `Element` into a plain old DOM node.
22+fromElement : Element -> JSDomNode
3823
39−--}
24+-- Turn a DOM node into an `Element`. You can resize the node
25+-- using the normal `width` and `height` functions.
26+toElement : Int -> Int -> JSDomNode -> Element
libraries/JavaScript/Experimental.elm+21
@@ -3,8 +3,29 @@ module JavaScript.Experimental where
33
44import JavaScript as JS
55
6+-- Turn arbitrary JavaScript objects into Elm records.
7+-- Arrays are converted into lists, nested objects are allowed.
8+-- No `null` values or non-homogeneous arrays.
9+--
10+-- -- OK objects
11+-- { student:"Steve", scores:[83,94,99,72] }
12+-- { errorLevel:10, critical:true }
13+--
14+-- -- BAD objects
15+-- { answer = null }
16+-- { info = [true,42,'what'] }
617toRecord : JSObject -> a
718toRecord = JS.toRecord
819
20+-- Turn arbitrary Elm records into JavaScript objects.
21+-- Lists become arrays, nested records are allowed. No ADTs.
22+--
23+-- -- OK records
24+-- { student="Steve", scores=[83,94,99,72] }
25+-- { errorLevel=10, critical=True }
26+--
27+-- -- BAD records
28+-- { answer = Nothing }
29+-- { result = Left "An error occurred" }
930fromRecord : a -> JSObject
1031fromRecord = JS.fromRecord
libraries/Keyboard.elm+6−6
@@ -1,17 +1,17 @@
1−-- These are nicely curated inputs from the keyboard. See the
2−-- [Keyboard.Raw library](/docs/Signal/KeyboardRaw.elm) for a
3−-- lower-level interface that will let you define more complicated behavior.
41module Keyboard where
52
63import Native.Keyboard as N
74
5+-- Type alias to make it clearer what integers are supposed to represent
6+-- in this library. Use [`Char.toCode`](docs/Char.elm#toCode) and
7+-- [`Char.fromCode`](/docs/Char.elm#fromCode) to convert key codes to characters.
88type KeyCode = Int
99
1010-- A signal of records indicating which arrow keys are pressed.
1111--
12−-- `{ x = 0, y = 0 }` when pressing no arrows.
13−-- `{ x =-1, y = 0 }` when pressing the left arrow.
14−-- `{ x = 1, y = 1 }` when pressing the up and right arrows.
12+-- `{ x = 0, y = 0 }` when pressing no arrows.<br>
13+-- `{ x =-1, y = 0 }` when pressing the left arrow.<br>
14+-- `{ x = 1, y = 1 }` when pressing the up and right arrows.<br>
1515-- `{ x = 0, y =-1 }` when pressing the down, left, and right arrows.
1616arrows : Signal { x:Int, y:Int }
1717arrows = N.directions 38 40 37 39
libraries/List.elm+40−22
@@ -4,29 +4,34 @@ module List where
44import Native.Utils (min, max)
55import Native.List as L
66
7−-- Add an element to the front of a list
8−-- a :: [b,c] = [a,b,c]
7+-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
98(::) : a -> [a] -> [a]
109
11−-- Appends two lists.
10+-- Puts two appendable things together:
11+--
12+-- [1,1] ++ [2,3] == [1,1,2,3]
13+-- "abc" ++ "123" == "abc123"
1214(++) : Appendable a -> Appendable a -> Appendable a
1315
1416-- Extract the first element of a list. List must be non-empty.
17+-- `(head [1,2,3] == 1)`
1518head : [a] -> a
1619
1720-- Extract the elements after the head of the list. List must be non-empty.
21+-- `(tail [1,2,3] == [2,3])`
1822tail : [a] -> [a]
1923
2024-- Extract the last element of a list. List must be non-empty.
25+-- `(last [1,2,3] == 3)`
2126last : [a] -> a
2227
23−-- Apply a function to every element of a list.
28+-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
2429map : (a -> b) -> [a] -> [b]
2530
26−-- Reduce a list from the left.
31+-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
2732foldl : (a -> b -> b) -> b -> [a] -> b
2833
29−-- Reduce a list from the right.
34+-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
3035foldr : (a -> b -> b) -> b -> [a] -> b
3136
3237-- Reduce a list from the left without a base case. List must be non-empty.
@@ -36,18 +41,22 @@ foldl1 : (a -> a -> a) -> [a] -> a
3641foldr1 : (a -> a -> a) -> [a] -> a
3742
3843-- Reduce a list from the left, building up all of the intermediate results into a list.
44+--
45+-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
3946scanl : (a -> b -> b) -> b -> [a] -> [b]
4047
4148-- Same as scanl but it doesn't require a base case. List must be non-empty.
49+--
50+-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
4251scanl1 : (a -> a -> a) -> [a] -> [a]
4352
44−-- Filter out elements which do not satisfy the predicate.
53+-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
4554filter : (a -> Bool) -> [a] -> [a]
4655
47−-- Determine the length of a list.
56+-- Determine the length of a list: `(length "innumerable" == 11)`
4857length : [a] -> Int
4958
50−-- Reverse a list.
59+-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
5160reverse : [a] -> [a]
5261
5362-- Check to see if all elements satisfy the predicate.
@@ -62,29 +71,31 @@ and : [Bool] -> Bool
6271-- Check to see if any elements are True.
6372or : [Bool] -> Bool
6473
65−-- Flatten a list of lists.
74+-- Concatenate a list of appendable things:
75+--
76+-- concat ["tree","house"] == "treehouse"
6677concat : [Appendable a] -> Appendable a
6778
6879-- Map a given function onto a list and flatten the resulting lists.
6980--
70−-- concatMap f xs == concat (map f xs)
81+-- concatMap f xs == concat (map f xs)
7182concatMap : (a -> Appendable b) -> [a] -> Appendable b
7283concatMap f = L.concat . L.map f
7384
74−-- Get the sum of the list elements.
85+-- Get the sum of the list elements. `(sum [1..4] == 10)`
7586sum : [Number a] -> Number a
7687sum = L.foldl (+) 0
7788
78−-- Get the product of the list elements.
89+-- Get the product of the list elements. `(product [1..4] == 24)`
7990product : [Number a] -> Number a
8091product = L.foldl (*) 1
8192
8293-- Find the highest number in a non-empty list.
83−maximum : [Comparable a] -> Comparable a
94+maximum : [Number a] -> Number a
8495maximum = L.foldl1 max
8596
8697-- Find the lowest number in a non-empty list.
87−minimum : [Comparable a] -> Comparable a
98+minimum : [Number a] -> Number a
8899minimum = L.foldl1 min
89100
90101-- Split a list based on the predicate.
@@ -95,13 +106,20 @@ partition pred lst =
95106 x::xs -> let (bs,cs) = partition pred xs in
96107 if pred x then (x::bs,cs) else (bs,x::cs)
97108
98−-- Combine two lists, combining them into tuples pairwise. If one input list has extra elements (it is longer), those elements are dropped.
109+-- Combine two lists, combining them into tuples pairwise.
110+-- If one list is longer, the extra elements are dropped.
111+--
112+-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
113+-- zip == zipWith (,)
99114zip : [a] -> [b] -> [(a,b)]
100115
101−-- Combine two lists, combining them with the given function. If one input list has extra elements (it is longer), those elements are dropped.
116+-- Combine two lists, combining them with the given function.
117+-- If one list is longer, the extra elements are dropped.
118+--
119+-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
102120zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
103121
104−-- Decompose a list of tuples
122+-- Decompose a list of tuples.
105123unzip : [(a,b)] -> ([a],[b])
106124unzip pairs =
107125 case pairs of
@@ -120,6 +138,8 @@ split : [a] -> [a] -> [[a]]
120138join : Appendable a -> [Appendable a] -> Appendable a
121139
122140-- Places the given value between all members of the given list.
141+--
142+-- intersperse ' ' "INCEPTION" == "I N C E P T I O N"
123143intersperse : a -> [a] -> [a]
124144intersperse sep xs =
125145 case xs of
@@ -127,11 +147,9 @@ intersperse sep xs =
127147 [a] -> [a]
128148 [] -> []
129149
130−-- Take the first n members of a list.
131−-- take 2 [1,2,3,4]) ==> [1,2]
150+-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
132151take : Int -> [a] -> [a]
133152
134−-- Drop the first n members of a list.
135−-- drop 2 [1,2,3,4]) ==> [3,4]
153+-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
136154drop : Int -> [a] -> [a]
137155
libraries/Matrix2D.elm+29−11
@@ -3,16 +3,34 @@ module Matrix2D where
33
44import Native.Matrix2D as M
55
6−identity : Matrix
6+-- Create an identity matrix.
7+--
8+-- / 1 0 \\
9+-- \\ 0 1 /
10+identity : Matrix2D
711
8−transform : Float -> Float -> Float -> Float -> Float -> Float -> Matrix -> Matrix
9−rotate : Float -> Matrix -> Matrix
10−move : (Float,Float) -> Matrix -> Matrix
11−scale : Float -> Matrix -> Matrix
12−scaleX : Float -> Matrix -> Matrix
13−scaleY : Float -> Matrix -> Matrix
14−reflectX : Matrix -> Matrix
15−reflectY : Matrix -> Matrix
12+-- Creates an arbitrary matrix. This lets you create scales, shears, reflections,
13+-- translations, or any other 2D transform.
14+--
15+-- matrix a b c d dx dy
16+--
17+-- / a b \\
18+-- \\ c d /
19+--
20+-- And `dx` and `dy` are the translation values.
21+matrix : Float -> Float -> Float -> Float -> Float -> Float -> Matrix2D
1622
17−multiply : Matrix -> Matrix -> Matrix
18−matrix : Float -> Float -> Float -> Float -> Float -> Float -> Matrix
23+-- Creates a [rotation matrix](http://en.wikipedia.org/wiki/Rotation_matrix).
24+-- Given an angle t, it creates a counterclockwise rotation matrix:
25+--
26+-- / cos t -sin t \\
27+-- \\ sin t cos t /
28+rotation : Float -> Matrix2D
29+
30+-- Multiplies two matrices together:
31+--
32+-- multiply a b
33+--
34+-- / a11 a12 \\ . / b11 b12 \\
35+-- \\ a21 a22 / \\ b21 b22 /
36+multiply : Matrix2D -> Matrix2D -> Matrix2D
libraries/Mouse.elm−2
@@ -12,7 +12,6 @@ x : Signal Int
1212-- The current y-coordinate of the mouse.
1313y : Signal Int
1414
15−
1615-- The current state of the left mouse-button.
1716-- True when the button is down, and false otherwise.
1817isDown : Signal Bool
@@ -20,7 +19,6 @@ isDown : Signal Bool
2019-- True immediately after the left mouse-button has been clicked,
2120-- and false otherwise.
2221isClicked : Signal Bool
23−
2422
2523-- Always equal to unit. Event triggers on every mouse click.
2624clicks : Signal ()
libraries/Native/Color.js+13−7
@@ -6,19 +6,25 @@ Elm.Native.Color = function(elm) {
66 elm.Native = elm.Native || {};
77 if (elm.Native.Color) return elm.Native.Color;
88
9+ var Utils = Elm.Native.Utils(elm);
10+
911 function complement(rgb) {
10− var hsv = toHSV(rgb);
11− hsv.hue = (hsv.hue + 180) % 360;
12− return toRGB(hsv);
12+ var hsv = toHSV(rgb);
13+ hsv.hue = (hsv.hue + 180) % 360;
14+ return toRGB(hsv);
1315 }
1416
1517 function hsva(h,s,v,a) {
16− var clr = toRGB({hue:h, saturation:s, value:v});
17− clr._3 = a;
18− return clr;
18+ var degree = A2(Utils.mod, h * 180 / Math.PI, 360);
19+ var clr = toRGB({hue:degree, saturation:s, value:v});
20+ clr._3 = a;
21+ return clr;
1922 }
2023
21− function hsv(h,s,v) { return toRGB({hue:h, saturation:s, value:v}); }
24+ function hsv(h,s,v) {
25+ var degree = A2(Utils.mod, h * 180 / Math.PI, 360);
26+ return toRGB({hue:degree, saturation:s, value:v});
27+ }
2228
2329 function toHSV(rgb) {
2430 var hsv = {};
libraries/Native/Graphics/Input.js−1
@@ -31,7 +31,6 @@ Elm.Native.Graphics.Input = function(elm) {
3131 node.elmEvent = newModel.event;
3232 var txt = newModel.text;
3333 if (oldModel.text !== txt) node.innerHTML = txt;
34− return true;
3534 }
3635
3736 function button(evnt, txt) {
libraries/Native/JavaScript.js+24−3
@@ -6,6 +6,7 @@ Elm.Native.JavaScript = function(elm) {
66 if (elm.Native.JavaScript) return elm.Native.JavaScript;
77
88 var List = Elm.Native.List(elm);
9+ var Render = ElmRuntime.use(ElmRuntime.Render.Element);
910
1011 function fromJS(v) {
1112 var type = typeof v;
@@ -61,7 +62,24 @@ Elm.Native.JavaScript = function(elm) {
6162 throw new Error("'fromRecord' must be called on a record.");
6263 }
6364
64− function id(n) { return n }
65+ function id(n) { return n; }
66+
67+ function toElement(w,h,domNode) {
68+ return A3( newElement, w, h, {
69+ ctor: 'Custom',
70+ type: 'DomNode',
71+ render: function(node) { return node; },
72+ update: function(node,oldNode,newNode) {
73+ if (node === newNode) return;
74+ node.parentNode.replaceChild(newNode, node);
75+ },
76+ model: domNode
77+ });
78+ }
79+
80+ function fromElement(element) {
81+ return Render.render(element);
82+ }
6583
6684 return elm.Native.JavaScript = {
6785 toFloat : id,
@@ -74,8 +92,11 @@ Elm.Native.JavaScript = function(elm) {
7492 fromInt : id,
7593 fromFloat : id,
7694 fromBool : id,
77− toRecord : fromJS,
78− fromRecord : fromRecord
95+
96+ toElement : toElement,
97+ fromElement : fromElement,
98+ toRecord : fromJS,
99+ fromRecord : fromRecord
79100 };
80101
81102};
libraries/Native/Matrix2D.js+12−4
@@ -17,9 +17,14 @@ Elm.Native.Matrix2D = function(elm) {
1717 // new A([ m11, m12, dx, m21, m22, dy ])
1818
1919 var identity = new A([1,0,0,0,1,0]);
20− function matrix(m11, m21, m12, m22, dx, dy) {
20+ function matrix(m11, m12, m21, m22, dx, dy) {
2121 return new A([m11, m12, dx, m21, m22, dy]);
2222 }
23+ function rotation(t) {
24+ var c = Math.cos(t);
25+ var s = Math.sin(t);
26+ return new A([c, -s, 0, s, c, 0]);
27+ }
2328
2429 function rotate(t,m) {
2530 var c = Math.cos(t);
@@ -28,6 +33,7 @@ Elm.Native.Matrix2D = function(elm) {
2833 return new A([m11*c + m12*s, -m11*s + m12*c, m[2],
2934 m21*c + m22*s, -m21*s + m22*c, m[5]]);
3035 }
36+ /*
3137 function move(xy,m) {
3238 var x = xy._0;
3339 var y = xy._1;
@@ -50,7 +56,7 @@ Elm.Native.Matrix2D = function(elm) {
5056 m21*n12 + m22*n22,
5157 m21*ndx + m22*ndy + mdy]);
5258 }
53−
59+ */
5460 function multiply(m, n) {
5561 return transform(m[0], m[3], m[1], m[4], m[2], m[5], n);
5662 }
@@ -58,8 +64,9 @@ Elm.Native.Matrix2D = function(elm) {
5864 return elm.Native.Matrix2D = {
5965 identity:identity,
6066 matrix:F6(matrix),
61− multiply:F2(multiply),
62−
67+ rotation:rotation,
68+ multiply:F2(multiply)
69+ /*
6370 transform:F7(transform),
6471 rotate:F2(rotate),
6572 move:F2(move),
@@ -68,6 +75,7 @@ Elm.Native.Matrix2D = function(elm) {
6875 scaleY:F2(scaleY),
6976 reflectX:reflectX,
7077 reflectY:reflectY
78+ */
7179 };
7280
7381};
libraries/Native/Prelude.js+1−6
@@ -10,12 +10,7 @@ Elm.Native.Prelude = function(elm) {
1010
1111 function div(a,b) { return (a/b)|0; }
1212 function rem(a,b) { return a % b; }
13− function mod(a,b) {
14− var r = a % b;
15− var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
16−
17− return m === b ? 0 : m;
18− }
13+ var mod = Utils.mod;
1914 function abs(x) { return x < 0 ? -x : x; }
2015 function logBase(base,n) { return Math.log(n) / Math.log(base); }
2116 function min(a,b) { return a < b ? a : b; }
libraries/Native/Utils.js+8
@@ -98,6 +98,13 @@ Elm.Native.Utils = function(elm) {
9898 function max(a,b) { return a > b ? a : b }
9999 function min(a,b) { return a < b ? a : b }
100100
101+ function mod(a,b) {
102+ var r = a % b;
103+ var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
104+
105+ return m === b ? 0 : m;
106+ }
107+
101108 function htmlHeight(width, html) {
102109 var t = document.createElement('div');
103110 t.innerHTML = html;
@@ -143,6 +150,7 @@ Elm.Native.Utils = function(elm) {
143150 guid: guid,
144151 max : F2(max),
145152 min : F2(min),
153+ mod : F2(mod),
146154 htmlHeight: F2(htmlHeight),
147155 toFloat: function(x){return x}
148156 };
libraries/Signal.elm+4−2
@@ -95,6 +95,8 @@ sampleOn : Signal a -> Signal b -> Signal b
9595
9696-- An alias for `lift`. A prettier way to apply a
9797-- function to the current value of a signal.
98+--
99+-- lift f signal == f <~ signal
98100(<~) : (a -> b) -> Signal a -> Signal b
99101
100102-- Signal application. This takes two signals, holding a function and
@@ -102,6 +104,6 @@ sampleOn : Signal a -> Signal b -> Signal b
102104--
103105-- So the following expressions are equivalent:
104106--
105−-- scene <~ Mouse.x ~ Mouse.y
106−-- lift2 scene Mouse.x Mouse.y
107+-- scene <~ Window.dimensions ~ Mouse.position
108+-- lift2 scene Window.dimensions Mouse.position
107109(~) : Signal (a -> b) -> Signal a -> Signal b
libraries/Text.elm+5−3
@@ -56,8 +56,10 @@ text : Text -> Element
5656
5757-- Display a plain string.
5858plainText : String -> Element
59−
59+
6060-- Convert anything to it's textual representation and make it displayable in browser
61−-- asText = text . monospace . show
62−-- Excellent for debugging
61+--
62+-- asText == text . monospace . show
63+--
64+-- Excellent for debugging.
6365asText : a -> Element
libraries/Time.elm+8−6
@@ -3,13 +3,15 @@ module Time where
33
44import Native.Time as T
55
6+-- Type alias to make it clearer when you are working with time values.
7+-- Using the `Time` constants instead of raw numbers is very highly recommended.
68type Time = Float
79
810-- Units of time, making it easier to specify things like a
9−-- half-second `(second / 2)`.
10−
11−ms : Time
12−ms = 1
11+-- half-second `(500 * milliseconds)` without remembering Elm&rsquo;s
12+-- underlying units of time.
13+millisecond : Time
14+millisecond = 1
1315
1416second : Time
1517second = 1000 * ms
@@ -20,8 +22,8 @@ minute = 60 * second
2022hour : Time
2123hour = 60 * minute
2224
23−inMss : Time -> Float
24−inMss t = t
25+inMilliseconds : Time -> Float
26+inMilliseconds t = t
2527
2628inSeconds : Time -> Float
2729inSeconds t = t / second
libraries/Touch.elm+8−6
@@ -4,13 +4,15 @@ module Touch where
44
55import Native.Touch as T
66
7−-- type Touch = { x:Int, y:Int, id:Int, x0:Int, y0:Int, t0:Time }
7+-- Every `Touch` has `xy` coordinates. It also has an identifier `id` to
8+-- distinguish one touch from another.
9+--
10+-- A touch also keeps info about the intial point and time of contact:
11+-- `x0`, `y0`, and `t0`. This helps compute more complicated gestures
12+-- like taps, drags, and swipes which need to know about timing or direction.
13+type Touch = { x:Int, y:Int, id:Int, x0:Int, y0:Int, t0:Time }
814
9−-- A list of touches. Each ongoing touch is represented by a set of
10−-- coordinates and an identifier id that allows you to distinguish
11−-- between different touches. Each touch also contains the coordinates and
12−-- time of the initial contact (x0, y0, and t0) which helps compute more
13−-- complicated gestures.
15+-- A list of ongoing touches.
1416touches : Signal [Touch]
1517
1618-- The last position that was tapped. Default value is `{x=0,y=0}`.