on master help sign in
Begin upgrading the List library. It is not complete though. List.js should be the final authority on what is native and what is not.
dd9d219evancz
core-elm/List.elm+59−30
@@ -1,59 +1,88 @@
1
1
2−head (h:t) = h
2+module List where
3−tail (h:t) = t
4
3
5−map f lst = case lst of { x:xs -> f x : map f xs; [] -> [] }
4+import Native.List as Native
6−foldl f b lst = case lst of { x:xs -> foldl f (f x b) xs; [] -> b }
7−foldr f b lst = case lst of { x:xs -> f x (foldr f b xs); [] -> b }
8
5
9−foldl1 f (x:xs) = foldl f x xs
6+head : [a] -> a
10−foldr1 f (x:xs) = foldr f x xs
7+head = Native.head
11
8
12−scanl f b xs = b : (case xs of { x:xs -> scanl f (f x b) xs; [] -> [] })
9+tail : [a] -> [a]
13−scanl1 f lst = case lst of { x:xs -> scanl f x xs; [] -> [] }
10+tail = Native.tail
14
11
15−filter pred lst =
12+map : (a -> b) -> [a] -> [b]
16− case lst of
13+map = Native.map
17− { x:xs -> if pred x then x : filter pred xs else filter pred xs
14+
18− ; [] -> [] }
15+foldl : (a -> b -> b) -> b -> [a] -> b
16+foldl = Native.foldl
17+
18+foldr : (a -> b -> b) -> b -> [a] -> b
19+foldr = Native.foldr
20+
21+foldl1 : (a -> a -> a) -> [a] -> a
22+foldl1 f (x::xs) = foldl f x xs
23+
24+foldr1 : (a -> a -> a) -> [a] -> a
25+foldr1 f (x::xs) = foldr f x xs
26+
27+scanl f b xs = b :: (case xs of { x::xs -> scanl f (f x b) xs; [] -> [] })
28+scanl1 f lst = case lst of { x::xs -> scanl f x xs; [] -> [] }
19
29
20−length = foldl (\x c -> 1 + c) 0
30+filter : (a -> Bool) -> [a] -> [a]
31+filter = Native.filter
32+
33+length : [a] -> Int
34+length = Native.length
35+
36+reverse : [a] -> [a]
21−reverse = foldl (:) []
37+reverse = foldl (::) []
22
38
23concat = foldr (++) []
39concat = foldr (++) []
24concatMap f = concat . map f
40concatMap f = concat . map f
25
41
42+and : [Bool] -> Bool
26and = foldl (&&) True
43and = foldl (&&) True
44+
45+or : [Bool] -> Bool
27or = foldl (||) False
46or = foldl (||) False
28
47
48+any : (a -> Bool) -> [a] -> Bool
29−forall pred = foldl (\x acc -> acc && pred x) True
49+any pred = foldl (\x acc -> acc && pred x) True
50+
51+all : (a -> Bool) -> [a] -> Bool
30−exists pred = foldl (\x acc -> acc || pred x) False
52+all pred = foldl (\x acc -> acc || pred x) False
31
53
32sum = foldl (+) 0
54sum = foldl (+) 0
33product = foldl (*) 1
55product = foldl (*) 1
34maximum = foldl1 max
56maximum = foldl1 max
35minimum = foldl1 min
57minimum = foldl1 min
36
58
59+partition : (a -> Bool) -> [a] -> ([a],[a])
37partition pred lst =
60partition pred lst =
38 case lst of
61 case lst of
62+ [] -> ([],[])
39− { x:xs -> case partition pred xs of
63+ x::xs -> let (as,bs) = partition pred xs in
40− { (as,bs) -> if pred x then (x:as,bs) else (as,x:bs) }
64+ if pred x then (x::as,bs) else (as,x::bs)
41− ; [] -> ([],[]) }
42
65
43−zipWith f listA listB = case (listA, listB) of
66+zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
44− { (a:as, b:bs) -> f a b : zipWith f as bs; _ -> [] }
67+zipWith = Native.zipWith
45
68
46−zip listA listB = case (listA, listB) of
69+zip : [a] -> [b] -> [(a,b)]
47− { (a:as, b:bs) -> (a,b) : zip as bs; _ -> [] }
70+zip = Native.zip
48
71
72+unzip : [(a,b)] -> ([a],[b])
49unzip pairs =
73unzip pairs =
50 case pairs of
74 case pairs of
51− { p:ps -> case (p, unzip ps) of { ((x,y),(xs,ys)) -> (x:xs,y:ys) }
75+ [] -> ([],[])
52− ; [] -> ([],[]) }
76+ (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
53
77
54intersperse sep xs =
78intersperse sep xs =
79+ case xs of
55− case xs of { a:b:cs -> a : sep : intersperse sep (b:cs)
80+ a::b::cs -> a :: sep :: intersperse sep (b::cs)
56− ; a:[] -> [a] ; [] -> [] }
81+ [a] -> [a]
82+ [] -> []
83+
57intercalate sep xs =
84intercalate sep xs =
85+ case xs of
58− case xs of { a:b:cs -> a ++ sep ++ intercalate sep (b:cs)
86+ a::b::cs -> a ++ sep ++ intercalate sep (b::cs)
59− ; a:[] -> a ; [] -> [] }
87+ [a] -> a
88+ [] -> []