on master help sign in
Get rid of `split` and make `repeat` significantly faster
ab8ff11Evan Czaplicki
libraries/List.elm−7
@@ -191,13 +191,6 @@ unzip pairs =
191 [] -> ([],[])
191 [] -> ([],[])
192 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
192 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
193
193
194−{-| Split a list with a given seperator.
195−
196− split "," "hello,there,friend" == ["hello", "there", "friend"]
197−-}
198−split : [a] -> [a] -> [[a]]
199−split = Native.List.split
200−
201{-| Places the given value between all of the lists in the second
194{-| Places the given value between all of the lists in the second
202argument and concatenates the result.
195argument and concatenates the result.
203
196
libraries/Native/List.js+9−64
@@ -241,13 +241,14 @@ Elm.Native.List.make = function(elm) {
241 return xs;
241 return xs;
242 }
242 }
243
243
244− function repeat(n, a) {
244+ function repeat(n, x) {
245− var arr = [];
245+ var arr = [];
246+ var pattern = [x];
246− while (n > 0) {
247+ while (n > 0) {
247− arr.push(a);
248+ if (n & 1) arr += pattern;
248− --n;
249+ n >>= 1, pattern += pattern;
249− }
250+ }
250− return fromArray(arr);
251+ return fromArray(arr);
251 }
252 }
252
253
253 function join(sep, xss) {
254 function join(sep, xss) {
@@ -263,61 +264,6 @@ Elm.Native.List.make = function(elm) {
263 return fromArray(out);
264 return fromArray(out);
264 }
265 }
265
266
266− function split(seperator, list) {
267− var array = toArray(list);
268− var alen = array.length;
269− if (alen === 0) {
270− // splitting an empty list is a list of lists: [[]]
271− return Cons(Nil,Nil);
272− }
273−
274− var sep = toArray(seperator);
275− var seplen = sep.length;
276− if (seplen === 0) {
277− // splitting with an empty sep is a list of all elements
278− // Same as (map (\x -> [x]) list)
279− var out = Nil;
280− for (var i = alen; i--; ) {
281− out = Cons(Cons(array[i],Nil), out);
282− }
283− return out;
284− }
285−
286− var matches = [-seplen];
287− var sepStart = sep[0];
288− var len = alen - seplen + 1;
289− for (var i = 0; i < len; ++i) {
290− if (Utils.eq(array[i], sepStart)) {
291− var match = true;
292− for (var j = seplen; --j; ) {
293− if (!Utils.eq(array[i+j], sep[j])) { match = false; break; }
294− }
295− if (match) {
296− matches.push(i);
297− i += seplen - 1;
298− }
299− }
300− }
301−
302− // shortcut in case of no matches
303− if (matches.length === 0) {
304− return Cons(list,Nil);
305− }
306−
307− var out = Nil;
308− var index = alen - 1;
309− for (var i = matches.length; i--; ) {
310− var temp = Nil;
311− var stop = matches[i] + seplen - 1;
312− for ( ; index > stop; --index ) {
313− temp = Cons(array[index], temp);
314− }
315− out = Cons(temp,out);
316− index -= seplen;
317− }
318− return out;
319− }
320−
321 Elm.Native.List.values = {
267 Elm.Native.List.values = {
322 Nil:Nil,
268 Nil:Nil,
323 Cons:Cons,
269 Cons:Cons,
@@ -355,8 +301,7 @@ Elm.Native.List.make = function(elm) {
355 drop:F2(drop),
301 drop:F2(drop),
356 repeat:F2(repeat),
302 repeat:F2(repeat),
357
303
358− join:F2(join),
304+ join:F2(join)
359− split:F2(split)
360 };
305 };
361 return elm.Native.List = Elm.Native.List.values;
306 return elm.Native.List = Elm.Native.List.values;
362
307