on master help sign in
Get rid of `split` and make `repeat` significantly faster
commitab8ff11960c6aba3cea4801c1d4a9918bc8c2e0a authorEvan Czaplicki <info@elm-lang.org> dateWed 2 Oct 2013, 01:02 (13 years ago) parent3459672
2 files changed+9−71
libraries/List.elm−7 libraries/Native/List.js+9−64
libraries/List.elm−7
@@ -191,13 +191,6 @@ unzip pairs =
191191 [] -> ([],[])
192192 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
193193
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−
201194{-| Places the given value between all of the lists in the second
202195argument and concatenates the result.
203196
libraries/Native/List.js+9−64
@@ -241,13 +241,14 @@ Elm.Native.List.make = function(elm) {
241241 return xs;
242242 }
243243
244− function repeat(n, a) {
245− var arr = [];
246− while (n > 0) {
247− arr.push(a);
248− --n;
249− }
250− return fromArray(arr);
244+ function repeat(n, x) {
245+ var arr = [];
246+ var pattern = [x];
247+ while (n > 0) {
248+ if (n & 1) arr += pattern;
249+ n >>= 1, pattern += pattern;
250+ }
251+ return fromArray(arr);
251252 }
252253
253254 function join(sep, xss) {
@@ -263,61 +264,6 @@ Elm.Native.List.make = function(elm) {
263264 return fromArray(out);
264265 }
265266
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−
321267 Elm.Native.List.values = {
322268 Nil:Nil,
323269 Cons:Cons,
@@ -355,8 +301,7 @@ Elm.Native.List.make = function(elm) {
355301 drop:F2(drop),
356302 repeat:F2(repeat),
357303
358− join:F2(join),
359− split:F2(split)
304+ join:F2(join)
360305 };
361306 return elm.Native.List = Elm.Native.List.values;
362307