Skip to content

Commit

Permalink
Rewrite List functions in terms of Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhefner committed Feb 8, 2015
1 parent 8586ba0 commit 5f8829d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 84 deletions.
35 changes: 21 additions & 14 deletions src/List.elm
Expand Up @@ -50,6 +50,7 @@ The current sentiment is that it is already quite error prone once you get to
import Basics (..)
import Maybe
import Maybe ( Maybe(Just,Nothing) )
import Generator
import Native.List


Expand Down Expand Up @@ -113,7 +114,7 @@ member x xs =
-}
map : (a -> b) -> List a -> List b
map f xs =
foldr (\x acc -> (f x) :: acc) [] xs
Generator.fromList xs |> Generator.map f |> Generator.toList

{-| Same as `map` but the function is also applied to the index of each
element (starting at zero).
Expand All @@ -129,14 +130,16 @@ indexedMap f xs =
foldl (::) [] [1,2,3] == [3,2,1]
-}
foldl : (a -> b -> b) -> b -> List a -> b
foldl = Native.List.foldl
foldl f b xs =
Generator.fromList xs |> Generator.foldl f b

{-| Reduce a list from the right.
foldr (+) 0 [1,2,3] == 6
-}
foldr : (a -> b -> b) -> b -> List a -> b
foldr = Native.List.foldr
foldr f b xs =
Generator.fromList xs |> Generator.foldr f b

{-| Reduce a list from the left, building up all of the intermediate results into a list.
Expand Down Expand Up @@ -172,13 +175,8 @@ the successes.
filterMap String.toInt ["3", "4.0", "5", "hats"] == [3,5]
-}
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 -> x :: xs
Nothing -> xs
filterMap f xs =
Generator.fromList xs |> Generator.filterMap f |> Generator.toList

{-| Determine the length of a list.
Expand Down Expand Up @@ -213,7 +211,8 @@ all pred xs =
any isEven [] == False
-}
any : (a -> Bool) -> List a -> Bool
any = Native.List.any
any pred xs =
Generator.fromList xs |> Generator.any pred


{-| Put two lists together.
Expand Down Expand Up @@ -360,21 +359,29 @@ intersperse sep xs =
take 2 [1,2,3,4] == [1,2]
-}
take : Int -> List a -> List a
take = Native.List.take
take n xs =
Generator.fromList xs |> Generator.take n

{-| Drop the first *n* members of a list.
drop 2 [1,2,3,4] == [3,4]
-}
drop : Int -> List a -> List a
drop = Native.List.drop
drop n xs =
let drop1 _ xs' =
case xs' of
_ :: rest -> rest
_ -> xs'
in
Generator.nil |> Generator.limit n |> Generator.foldl drop1 xs

{-| Create a list with *n* copies of a value:
repeat 3 (0,0) == [(0,0),(0,0),(0,0)]
-}
repeat : Int -> a -> List a
repeat = Native.List.repeat
repeat n x =
Generator.repeat x |> Generator.take n

{-| Sort values from lowest to highest
Expand Down
70 changes: 0 additions & 70 deletions src/Native/List.js
Expand Up @@ -42,37 +42,6 @@ Elm.Native.List.make = function(localRuntime) {
return lst
}

// f defined similarly for both foldl and foldr (NB: different from Haskell)
// ie, foldl : (a -> b -> b) -> b -> [a] -> b
function foldl(f, b, xs) {
var acc = b;
while (xs.ctor !== '[]') {
acc = A2(f, xs._0, acc);
xs = xs._1;
}
return acc;
}

function foldr(f, b, xs) {
var arr = toArray(xs);
var acc = b;
for (var i = arr.length; i--; ) {
acc = A2(f, arr[i], acc);
}
return acc;
}

function any(pred, xs) {
while (xs.ctor !== '[]') {
if (pred(xs._0))
{
return true;
}
xs = xs._1;
}
return false;
}

function map2(f, xs, ys) {
var arr = [];
while (xs.ctor !== '[]' && ys.ctor !== '[]') {
Expand Down Expand Up @@ -141,38 +110,6 @@ Elm.Native.List.make = function(localRuntime) {
}));
}

function take(n, xs) {
var arr = [];
while (xs.ctor !== '[]' && n > 0) {
arr.push(xs._0);
xs = xs._1;
--n;
}
return fromArray(arr);
}

function drop(n, xs) {
while (xs.ctor !== '[]' && n > 0) {
xs = xs._1;
--n;
}
return xs;
}

function repeat(n, x) {
var arr = [];
var pattern = [x];
while (n > 0) {
if (n & 1)
{
arr = arr.concat(pattern);
}
n >>= 1, pattern = pattern.concat(pattern);
}
return fromArray(arr);
}


Elm.Native.List.values = {
Nil:Nil,
Cons:Cons,
Expand All @@ -181,19 +118,12 @@ Elm.Native.List.make = function(localRuntime) {
fromArray:fromArray,
range:range,

foldl:F3(foldl),
foldr:F3(foldr),

any:F2(any),
map2:F3(map2),
map3:F4(map3),
map4:F5(map4),
map5:F6(map5),
sortBy:F2(sortBy),
sortWith:F2(sortWith),
take:F2(take),
drop:F2(drop),
repeat:F2(repeat)
};
return localRuntime.Native.List.values = Elm.Native.List.values;

Expand Down

0 comments on commit 5f8829d

Please sign in to comment.