on master help sign in
Improve documentation a bit and get `split` working properly.
commit5044724cda54090ae2792edad246d0188d4ef1ad authorevancz <info@elm-lang.org> dateFri 17 May 2013, 12:49 (13 years ago) parent6c0dc8e
5 files changed+94−58
libraries/Char.elm+5−5 libraries/List.elm+3−3 libraries/Native/List.js+52−26 libraries/Native/Signal/Time.js−2 libraries/Prelude.elm+34−22
libraries/Char.elm+5−5
@@ -3,19 +3,19 @@ module Char where
33
44import Native.Char as N
55
6−-- Selects upper case letters.
6+-- True for upper case letters.
77isUpper : Char -> Bool
88
9−-- Selects lower case letters.
9+-- True for lower case letters.
1010isLower : Char -> Bool
1111
12−-- Selects ASCII digits (0..9).
12+-- True for ASCII digits (`0..9`).
1313isDigit : Char -> Bool
1414
15−-- Selects ASCII octal digits (0..7).
15+-- True for ASCII octal digits (`0..7`).
1616isOctDigit : Char -> Bool
1717
18−-- Selects ASCII hexadecimal digits (0..9a..fA..F).
18+-- True for ASCII hexadecimal digits (`0..9a..fA..F`).
1919isHexDigit : Char -> Bool
2020
2121-- Convert to upper case.
libraries/List.elm+3−3
@@ -108,15 +108,15 @@ unzip pairs =
108108 [] -> ([],[])
109109 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
110110
111−-- Split a list
111+-- Split a list with a given seperator.
112112--
113−-- split "," "hello,there,friend" == ["hello", "there", "friend"]
113+-- split "," "hello,there,friend" == ["hello", "there", "friend"]
114114split : [a] -> [a] -> [[a]]
115115
116116-- Places the given value between all of the lists in the second argument
117117-- and concatenates the result.
118118--
119−-- join xs xss = concat (intersperse xs xss)
119+-- join "a" ["H","w","ii","n"] == "Hawaiian"
120120join : Appendable a -> [Appendable a] -> Appendable a
121121
122122-- 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) {
1717 // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
1818 // (on firefox 19)
1919
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' };
2321
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
2524 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
2625
2726 function throwError(f) {
@@ -251,35 +250,62 @@ Elm.Native.List = function(elm) {
251250 return fromArray(out);
252251 }
253252
254− function split(sep, xs) {
255− 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--; ) {
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)
266+ var out = Nil;
267+ for (var i = alen; i--; ) {
261268 out = Cons(Cons(array[i],Nil), out);
262269 }
263270 return out;
264271 }
265− var temp = Nil;
266− for (var i = array.length - slen; i >= 0; --i) {
267− var match = true;
268− for (var j = slen; j--; ) {
269− if (!Utils.eq(array[i+j], s[j])) { match = false; break; }
270− }
271− if (match) {
272− out = Cons(temp,out);
273− temp = Nil;
274− i -= slen - 1;
275− } else {
276− temp = Cons(array[i+j], temp);
272+
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)) {
278+ var match = true;
279+ for (var j = seplen; --j; ) {
280+ if (!Utils.eq(array[i+j], sep[j])) { match = false; break; }
281+ }
282+ if (match) {
283+ console.log(i);
284+ matches.push(i);
285+ i += seplen - 1;
286+ }
277287 }
278288 }
279− for (var j = slen-1; j--; ) {
280− temp = Cons(array[j], temp);
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 ) {
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;
281308 }
282− out = Cons(temp, out);
283309 return out;
284310 }
285311
libraries/Native/Signal/Time.js−2
@@ -53,8 +53,6 @@ Elm.Native.Time = function(elm) {
5353 delay : Signal.delay,
5454 timestamp : Signal.timestamp,
5555 since : F2(since),
56− after : after,
57− before : before,
5856 toDate : function(t) { return new window.Date(t); },
5957 read : read
6058 };
libraries/Prelude.elm+34−22
@@ -36,26 +36,31 @@ toPolar (x,y) = (N.sqrt (x^2 + y^2), N.atan2 y x)
3636-- Integer division, remainder is discarded.
3737div : Int -> Int -> Int
3838
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``
4040rem : Int -> Int -> Int
4141
42−-- Perform modular arithmetic: 7 `mod` 2 == 1
42+-- Perform modular arithmetic: ``7 `mod` 2 == 1``
4343mod : Int -> Int -> Int
4444
4545-- Exponentiation: `3^2 == 9`
4646(^) : Number a -> Number a -> Number a
4747
48−-- Basic functions of trigonometry.
4948cos : Float -> Float
5049sin : Float -> Float
5150tan : Float -> Float
5251acos : Float -> Float
5352asin : 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).
5457atan : Float -> Float
5558
56−-- Returns the arctangent of the quotient of its arguments in radians.
57−-- So `atan2 y x` computes the angle from the positive x-axis to the vector
58−-- starting at the origin and ending at (x,y).
59+-- This helps you find the angle of a cartesian coordinate.
60+-- You will almost certainly want to use this instead of `atan`.
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.
5964atan2 : Float -> Float -> Float
6065
6166-- Take the square root of a number.
@@ -67,12 +72,18 @@ abs : Number a -> Number a
6772-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
6873logBase : Number a -> Number a -> Number a
6974
70−-- Given two numbers, returns the smaller (or greater respectively).
75+-- Given two numbers, returns the smaller one.
7176min : Number a -> Number a -> Number a
77+
78+-- Given two numbers, returns the larger one.
7279max : Number a -> Number a -> Number a
7380
74−-- Clamps a number within a given range, so `clamp 100 200 x` is 200 for
75−-- x >= 200, 100 for x <= 100, and x for any 100 < x < 200
81+-- Clamps a number within a given range. With the expression `clamp 100 200 x`
82+-- the results are as follows:
83+--
84+-- * `100 if x < 100`
85+-- * ` x if 100 <= x < 200`
86+-- * `200 if 200 <= x`
7687clamp : Number a -> Number a -> Number a -> Number a
7788
7889-- An approximation of pi.
@@ -81,21 +92,22 @@ pi : Float
8192-- An approximation of e.
8293e : Float
8394
84−-- Compare any two values for structural equality and inequality.
85−-- Functions cannot be compared.
95+-- Compare any two values for structural equality. Functions cannot be compared.
8696(==) : a -> a -> Bool
8797(/=) : a -> a -> Bool
8898
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.
9299(<) : Comparable a -> Comparable a -> Bool
93100(>) : Comparable a -> Comparable a -> Bool
94101(<=) : Comparable a -> Comparable a -> Bool
95102(>=) : Comparable a -> Comparable a -> Bool
96103
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.
97107compare : Comparable a -> Comparable a -> Order
98108
109+-- Represents the relative ordering of two things.
110+-- The relations are less than, equal to, and greater than.
99111data Order = LT | EQ | GT
100112
101113-- The and operator. True if both inputs are True.
@@ -107,7 +119,7 @@ data Order = LT | EQ | GT
107119-- The exclusive-or operator. True if exactly one input is True.
108120xor : Bool -> Bool -> Bool
109121
110−-- Negate a boolean value: (not True == False) and (not False == True)
122+-- Negate a boolean value: `(not True == False)` and `(not False == True)`
111123not : Bool -> Bool
112124
113125-- Equal to true. Useful as the last case of a multi-way-if.
@@ -143,30 +155,30 @@ readFloat : String -> Maybe Float
143155
144156-- Function Helpers
145157
146−-- Function composition: f . g == (\x -> f (g x))
158+-- Function composition: `(f . g == (\\x -> f (g x)))`
147159(.) : (b -> c) -> (a -> b) -> (a -> c)
148160
149161-- Forward function application `x |> f == f x`. This function is useful
150162-- for avoiding parenthesis and writing code in a more natural way.
151163-- Consider the following code to create a pentagon:
152164--
153−-- scale 2 (move 100 100 (filled blue (ngon 5 30)))
165+-- scale 2 (move (10,10) (filled blue (ngon 5 30)))
154166--
155167-- This can also be written as:
156168--
157−-- ngon 5 30 |> filled blue
158−-- |> move 100 100
159−-- |> scale 2
169+-- ngon 5 30 |> filled blue
170+-- |> move (10,10)
171+-- |> scale 2
160172(|>) : a -> (a -> b) -> b
161173
162174-- Function application `f <| x == f x`. This function is useful for avoiding
163175-- parenthesis. Consider the following code to create a text element:
164176--
165−-- text (monospace (toText "code"))
177+-- text (monospace (toText "code"))
166178--
167179-- This can also be written as:
168180--
169−-- text . monospace <| toText "code"
181+-- text . monospace <| toText "code"
170182(<|) : (a -> b) -> a -> b
171183
172184-- Given a value, returns exactly the same value.