on master help sign in
Improve documentation a bit and get `split` working properly.
5044724evancz
libraries/Char.elm+5−5
@@ -3,19 +3,19 @@ module Char where
3
3
4import Native.Char as N
4import Native.Char as N
5
5
6−-- Selects upper case letters.
6+-- True for upper case letters.
7isUpper : Char -> Bool
7isUpper : Char -> Bool
8
8
9−-- Selects lower case letters.
9+-- True for lower case letters.
10isLower : Char -> Bool
10isLower : Char -> Bool
11
11
12−-- Selects ASCII digits (0..9).
12+-- True for ASCII digits (`0..9`).
13isDigit : Char -> Bool
13isDigit : Char -> Bool
14
14
15−-- Selects ASCII octal digits (0..7).
15+-- True for ASCII octal digits (`0..7`).
16isOctDigit : Char -> Bool
16isOctDigit : Char -> Bool
17
17
18−-- Selects ASCII hexadecimal digits (0..9a..fA..F).
18+-- True for ASCII hexadecimal digits (`0..9a..fA..F`).
19isHexDigit : Char -> Bool
19isHexDigit : Char -> Bool
20
20
21-- Convert to upper case.
21-- Convert to upper case.
libraries/List.elm+3−3
@@ -108,15 +108,15 @@ unzip pairs =
108 [] -> ([],[])
108 [] -> ([],[])
109 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
109 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
110
110
111−-- Split a list
111+-- Split a list with a given seperator.
112--
112--
113−-- split "," "hello,there,friend" == ["hello", "there", "friend"]
113+-- split "," "hello,there,friend" == ["hello", "there", "friend"]
114split : [a] -> [a] -> [[a]]
114split : [a] -> [a] -> [[a]]
115
115
116-- Places the given value between all of the lists in the second argument
116-- Places the given value between all of the lists in the second argument
117-- and concatenates the result.
117-- and concatenates the result.
118--
118--
119−-- join xs xss = concat (intersperse xs xss)
119+-- join "a" ["H","w","ii","n"] == "Hawaiian"
120join : Appendable a -> [Appendable a] -> Appendable a
120join : Appendable a -> [Appendable a] -> Appendable a
121
121
122-- Places the given value between all members of the given list.
122-- Places the given value between all members of the given list.
libraries/Native/List.js+52−26
@@ -17,11 +17,10 @@ Elm.Native.List = function(elm) {
17 // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
17 // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
18 // (on firefox 19)
18 // (on firefox 19)
19
19
20− // freeze is universally supported and as a singleton introduces little performance penalty
21− // for a small amount of object safety
22− var Nil = Object.freeze({ ctor:'Nil' });
20+ var Nil = { ctor:'Nil' };
23
21
24− // using freeze for every cons would be nice but is a huge (9x on firefox 19) performance penalty
22+ // using freeze for every cons would be nice but is a huge (9x on firefox 19)
23+ // performance penalty
25 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
24 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
26
25
27 function throwError(f) {
26 function throwError(f) {
@@ -251,35 +250,62 @@ Elm.Native.List = function(elm) {
251 return fromArray(out);
250 return fromArray(out);
252 }
251 }
253
252
254− function split(sep, xs) {
253+ function split(seperator, list) {
254+ var array = toArray(list);
255+ var alen = array.length;
256+ if (alen === 0) {
257+ // splitting an empty list is a list of lists: [[]]
258+ return Cons(Nil,Nil);
259+ }
260+
261+ var sep = toArray(seperator);
262+ var seplen = sep.length;
263+ if (seplen === 0) {
264+ // splitting with an empty sep is a list of all elements
265+ // Same as (map (\x -> [x]) list)
255− var out = Nil;
266+ var out = Nil;
256− var array = toArray(xs);
257− var s = toArray(sep);
258− var slen = s.length;
259− if (slen === 0) {
260− for (var i = array.length; i--; ) {
267+ for (var i = alen; i--; ) {
261 out = Cons(Cons(array[i],Nil), out);
268 out = Cons(Cons(array[i],Nil), out);
262 }
269 }
263 return out;
270 return out;
264 }
271 }
265− var temp = Nil;
272+
266− for (var i = array.length - slen; i >= 0; --i) {
273+ var matches = [-seplen];
274+ var sepStart = sep[0];
275+ var len = alen - seplen + 1;
276+ for (var i = 0; i < len; ++i) {
277+ if (Utils.eq(array[i], sepStart)) {
267− var match = true;
278+ var match = true;
268− for (var j = slen; j--; ) {
279+ for (var j = seplen; --j; ) {
269− if (!Utils.eq(array[i+j], s[j])) { match = false; break; }
280+ if (!Utils.eq(array[i+j], sep[j])) { match = false; break; }
270− }
281+ }
271− if (match) {
282+ if (match) {
272− out = Cons(temp,out);
283+ console.log(i);
273− temp = Nil;
284+ matches.push(i);
274− i -= slen - 1;
285+ i += seplen - 1;
275− } else {
286+ }
276− temp = Cons(array[i+j], temp);
277 }
287 }
278 }
288 }
279− for (var j = slen-1; j--; ) {
289+
290+ // shortcut in case of no matches
291+ if (matches.length === 0) {
292+ return Cons(list,Nil);
293+ }
294+
295+ var out = Nil;
296+ var index = alen - 1;
297+ for (var i = matches.length; i--; ) {
298+ console.log('starts at', index);
299+ var temp = Nil;
300+ var stop = matches[i] + seplen - 1;
301+ for ( ; index > stop; --index ) {
280− temp = Cons(array[j], temp);
302+ temp = Cons(array[index], temp);
303+ }
304+ console.log(toArray(temp));
305+ out = Cons(temp,out);
306+ console.log('ends at', index);
307+ index -= seplen;
281 }
308 }
282− out = Cons(temp, out);
283 return out;
309 return out;
284 }
310 }
285
311
libraries/Native/Signal/Time.js−2
@@ -53,8 +53,6 @@ Elm.Native.Time = function(elm) {
53 delay : Signal.delay,
53 delay : Signal.delay,
54 timestamp : Signal.timestamp,
54 timestamp : Signal.timestamp,
55 since : F2(since),
55 since : F2(since),
56− after : after,
57− before : before,
58 toDate : function(t) { return new window.Date(t); },
56 toDate : function(t) { return new window.Date(t); },
59 read : read
57 read : read
60 };
58 };
libraries/Prelude.elm+34−22
@@ -36,26 +36,31 @@ toPolar (x,y) = (N.sqrt (x^2 + y^2), N.atan2 y x)
36-- Integer division, remainder is discarded.
36-- Integer division, remainder is discarded.
37div : Int -> Int -> Int
37div : Int -> Int -> Int
38
38
39−-- Finds the remainder after dividing one number by another: 4 `rem` 3 == 1
39+-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
40rem : Int -> Int -> Int
40rem : Int -> Int -> Int
41
41
42−-- Perform modular arithmetic: 7 `mod` 2 == 1
42+-- Perform modular arithmetic: ``7 `mod` 2 == 1``
43mod : Int -> Int -> Int
43mod : Int -> Int -> Int
44
44
45-- Exponentiation: `3^2 == 9`
45-- Exponentiation: `3^2 == 9`
46(^) : Number a -> Number a -> Number a
46(^) : Number a -> Number a -> Number a
47
47
48−-- Basic functions of trigonometry.
49cos : Float -> Float
48cos : Float -> Float
50sin : Float -> Float
49sin : Float -> Float
51tan : Float -> Float
50tan : Float -> Float
52acos : Float -> Float
51acos : Float -> Float
53asin : Float -> Float
52asin : Float -> Float
53+
54+-- You probably do not want to use this. Because it takes `(y/x)` as the argument
55+-- there is no way to know where the negative signs come from so the resulting
56+-- angle is always between &pi;/2 and -&pi;/2 (in quadrants I and IV).
54atan : Float -> Float
57atan : Float -> Float
55
58
56−-- Returns the arctangent of the quotient of its arguments in radians.
59+-- This helps you find the angle of a cartesian coordinate.
57−-- So `atan2 y x` computes the angle from the positive x-axis to the vector
60+-- You will almost certainly want to use this instead of `atan`.
58−-- starting at the origin and ending at (x,y).
61+-- So `atan2 y x` computes *atan(y/x)* but also keeps track of which
62+-- quadrant the angle should really be in. The result will be between
63+-- &pi; and -&pi;, giving you the full range of angles.
59atan2 : Float -> Float -> Float
64atan2 : Float -> Float -> Float
60
65
61-- Take the square root of a number.
66-- Take the square root of a number.
@@ -67,12 +72,18 @@ abs : Number a -> Number a
67-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
72-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
68logBase : Number a -> Number a -> Number a
73logBase : Number a -> Number a -> Number a
69
74
70−-- Given two numbers, returns the smaller (or greater respectively).
75+-- Given two numbers, returns the smaller one.
71min : Number a -> Number a -> Number a
76min : Number a -> Number a -> Number a
77+
78+-- Given two numbers, returns the larger one.
72max : Number a -> Number a -> Number a
79max : Number a -> Number a -> Number a
73
80
74−-- Clamps a number within a given range, so `clamp 100 200 x` is 200 for
81+-- Clamps a number within a given range. With the expression `clamp 100 200 x`
75−-- x >= 200, 100 for x <= 100, and x for any 100 < x < 200
82+-- the results are as follows:
83+--
84+-- * `100 if x < 100`
85+-- * ` x if 100 <= x < 200`
86+-- * `200 if 200 <= x`
76clamp : Number a -> Number a -> Number a -> Number a
87clamp : Number a -> Number a -> Number a -> Number a
77
88
78-- An approximation of pi.
89-- An approximation of pi.
@@ -81,21 +92,22 @@ pi : Float
81-- An approximation of e.
92-- An approximation of e.
82e : Float
93e : Float
83
94
84−-- Compare any two values for structural equality and inequality.
95+-- Compare any two values for structural equality. Functions cannot be compared.
85−-- Functions cannot be compared.
86(==) : a -> a -> Bool
96(==) : a -> a -> Bool
87(/=) : a -> a -> Bool
97(/=) : a -> a -> Bool
88
98
89−-- Compare any two comparable values. Comparable values include String, Char,
90−-- Int, Float, Time, or a list or tuple containing comparable values.
91−-- These are also the only values that work as Dictionary keys or Set members.
92(<) : Comparable a -> Comparable a -> Bool
99(<) : Comparable a -> Comparable a -> Bool
93(>) : Comparable a -> Comparable a -> Bool
100(>) : Comparable a -> Comparable a -> Bool
94(<=) : Comparable a -> Comparable a -> Bool
101(<=) : Comparable a -> Comparable a -> Bool
95(>=) : Comparable a -> Comparable a -> Bool
102(>=) : Comparable a -> Comparable a -> Bool
96
103
104+-- Compare any two comparable values. Comparable values include `String`, `Char`,
105+-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
106+-- These are also the only values that work as `Dict` keys or `Set` members.
97compare : Comparable a -> Comparable a -> Order
107compare : Comparable a -> Comparable a -> Order
98
108
109+-- Represents the relative ordering of two things.
110+-- The relations are less than, equal to, and greater than.
99data Order = LT | EQ | GT
111data Order = LT | EQ | GT
100
112
101-- The and operator. True if both inputs are True.
113-- The and operator. True if both inputs are True.
@@ -107,7 +119,7 @@ data Order = LT | EQ | GT
107-- The exclusive-or operator. True if exactly one input is True.
119-- The exclusive-or operator. True if exactly one input is True.
108xor : Bool -> Bool -> Bool
120xor : Bool -> Bool -> Bool
109
121
110−-- Negate a boolean value: (not True == False) and (not False == True)
122+-- Negate a boolean value: `(not True == False)` and `(not False == True)`
111not : Bool -> Bool
123not : Bool -> Bool
112
124
113-- Equal to true. Useful as the last case of a multi-way-if.
125-- Equal to true. Useful as the last case of a multi-way-if.
@@ -143,30 +155,30 @@ readFloat : String -> Maybe Float
143
155
144-- Function Helpers
156-- Function Helpers
145
157
146−-- Function composition: f . g == (\x -> f (g x))
158+-- Function composition: `(f . g == (\\x -> f (g x)))`
147(.) : (b -> c) -> (a -> b) -> (a -> c)
159(.) : (b -> c) -> (a -> b) -> (a -> c)
148
160
149-- Forward function application `x |> f == f x`. This function is useful
161-- Forward function application `x |> f == f x`. This function is useful
150-- for avoiding parenthesis and writing code in a more natural way.
162-- for avoiding parenthesis and writing code in a more natural way.
151-- Consider the following code to create a pentagon:
163-- Consider the following code to create a pentagon:
152--
164--
153−-- scale 2 (move 100 100 (filled blue (ngon 5 30)))
165+-- scale 2 (move (10,10) (filled blue (ngon 5 30)))
154--
166--
155-- This can also be written as:
167-- This can also be written as:
156--
168--
157−-- ngon 5 30 |> filled blue
169+-- ngon 5 30 |> filled blue
158−-- |> move 100 100
170+-- |> move (10,10)
159−-- |> scale 2
171+-- |> scale 2
160(|>) : a -> (a -> b) -> b
172(|>) : a -> (a -> b) -> b
161
173
162-- Function application `f <| x == f x`. This function is useful for avoiding
174-- Function application `f <| x == f x`. This function is useful for avoiding
163-- parenthesis. Consider the following code to create a text element:
175-- parenthesis. Consider the following code to create a text element:
164--
176--
165−-- text (monospace (toText "code"))
177+-- text (monospace (toText "code"))
166--
178--
167-- This can also be written as:
179-- This can also be written as:
168--
180--
169−-- text . monospace <| toText "code"
181+-- text . monospace <| toText "code"
170(<|) : (a -> b) -> a -> b
182(<|) : (a -> b) -> a -> b
171
183
172-- Given a value, returns exactly the same value.
184-- Given a value, returns exactly the same value.