diff --git a/src/Elm/Kernel/Debug.js b/src/Elm/Kernel/Debug.js index d2b04c98..f68ef007 100644 --- a/src/Elm/Kernel/Debug.js +++ b/src/Elm/Kernel/Debug.js @@ -148,7 +148,7 @@ function _Debug_toAnsiString(ansi, value) return _Debug_stringColor(ansi, '<' + value.byteLength + ' bytes>'); } - if (typeof File === 'function' && value instanceof File) + if (typeof File !== 'undefined' && value instanceof File) { return _Debug_internalColor(ansi, '<' + value.name + '>'); } diff --git a/src/Elm/Kernel/List.js b/src/Elm/Kernel/List.js index 2441718b..40f31af7 100644 --- a/src/Elm/Kernel/List.js +++ b/src/Elm/Kernel/List.js @@ -70,6 +70,23 @@ var _List_map5 = F6(function(f, vs, ws, xs, ys, zs) return _List_fromArray(arr); }); +var _List_append = F2(_List_ap); + +function _List_ap(xs, ys) +{ + if (!xs.b) + { + return ys; + } + var root = _List_Cons(xs.a, ys); + xs = xs.b + for (var curr = root; xs.b; xs = xs.b) // WHILE_CONS + { + curr = curr.b = _List_Cons(xs.a, ys); + } + return root; +} + var _List_sortBy = F2(function(f, xs) { return _List_fromArray(_List_toArray(xs).sort(function(a, b) { diff --git a/src/Elm/Kernel/Utils.js b/src/Elm/Kernel/Utils.js index 66406990..74fc2613 100644 --- a/src/Elm/Kernel/Utils.js +++ b/src/Elm/Kernel/Utils.js @@ -178,16 +178,6 @@ function _Utils_ap(xs, ys) return xs + ys; } - // append Lists - if (!xs.b) - { - return ys; - } - var root = __List_Cons(xs.a, ys); - xs = xs.b - for (var curr = root; xs.b; xs = xs.b) // WHILE_CONS - { - curr = curr.b = __List_Cons(xs.a, ys); - } - return root; + // append Lists + return __List_ap(xs, ys); } diff --git a/src/List.elm b/src/List.elm index 9ef2b176..1fdb015f 100644 --- a/src/List.elm +++ b/src/List.elm @@ -132,7 +132,17 @@ element (starting at zero). -} indexedMap : (Int -> a -> b) -> List a -> List b indexedMap f xs = - map2 f (range 0 (length xs - 1)) xs + reverse (indexedMapHelper 0 f xs []) + + +indexedMapHelper : (Int -> a -> b)->Int -> List a -> List b -> List b +indexedMapHelper fn index list result = + case list of + [] -> + result + + x :: xs -> + indexedMapHelper fn (index + 1) xs (cons (fn index x) result) {-| Reduce a list from the left. @@ -198,7 +208,7 @@ foldrHelper fn acc ctr ls = d :: r4 -> let res = - if ctr > 500 then + if ctr > 1000 then foldl fn acc (reverse r4) else foldrHelper fn acc (ctr + 1) r4 @@ -227,17 +237,16 @@ from an untrusted source and you want to turn them into numbers: -} filterMap : (a -> Maybe b) -> List a -> List b filterMap f xs = - foldr (maybeCons f) [] xs - - -maybeCons : (a -> Maybe b) -> a -> List b -> List b -maybeCons f mx xs = - case f mx of - Just x -> - cons x xs + let + helper mx acc = + case f mx of + Just x -> + cons x acc - Nothing -> - xs + Nothing -> + acc + in + foldr helper [] xs -- UTILITIES @@ -363,13 +372,8 @@ product numbers = You can also use [the `(++)` operator](Basics#++) to append lists. -} append : List a -> List a -> List a -append xs ys = - case ys of - [] -> - xs - - _ -> - foldr cons ys xs +append = + Elm.Kernel.List.append {-| Concatenate a bunch of lists into a single list: @@ -387,7 +391,12 @@ concat lists = -} concatMap : (a -> List b) -> List a -> List b concatMap f list = - concat (map f list) + let + helper val acc = + append (f val) acc + in + foldr helper [] list + {-| Places the given value between all members of the given list.