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.
commitdd9d219655b31de95a5d119f3fd62d680efc41a3 authorevancz <info@elm-lang.org> dateFri 8 Feb 2013, 22:17 (14 years ago) parent43fd6c4
1 file changed+59−30
core-elm/List.elm+59−30
core-elm/List.elm+59−30
@@ -1,59 +1,88 @@
11
2−head (h:t) = h
3−tail (h:t) = t
2+module List where
43
5−map f lst = case lst of { x:xs -> f x : map f xs; [] -> [] }
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 }
4+import Native.List as Native
85
9−foldl1 f (x:xs) = foldl f x xs
10−foldr1 f (x:xs) = foldr f x xs
6+head : [a] -> a
7+head = Native.head
118
12−scanl f b xs = b : (case xs of { x:xs -> scanl f (f x b) xs; [] -> [] })
13−scanl1 f lst = case lst of { x:xs -> scanl f x xs; [] -> [] }
9+tail : [a] -> [a]
10+tail = Native.tail
1411
15−filter pred lst =
16− case lst of
17− { x:xs -> if pred x then x : filter pred xs else filter pred xs
18− ; [] -> [] }
12+map : (a -> b) -> [a] -> [b]
13+map = Native.map
14+
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; [] -> [] }
1929
20−length = foldl (\x c -> 1 + c) 0
21−reverse = foldl (:) []
30+filter : (a -> Bool) -> [a] -> [a]
31+filter = Native.filter
32+
33+length : [a] -> Int
34+length = Native.length
35+
36+reverse : [a] -> [a]
37+reverse = foldl (::) []
2238
2339concat = foldr (++) []
2440concatMap f = concat . map f
2541
42+and : [Bool] -> Bool
2643and = foldl (&&) True
44+
45+or : [Bool] -> Bool
2746or = foldl (||) False
2847
29−forall pred = foldl (\x acc -> acc && pred x) True
30−exists pred = foldl (\x acc -> acc || pred x) False
48+any : (a -> Bool) -> [a] -> Bool
49+any pred = foldl (\x acc -> acc && pred x) True
50+
51+all : (a -> Bool) -> [a] -> Bool
52+all pred = foldl (\x acc -> acc || pred x) False
3153
3254sum = foldl (+) 0
3355product = foldl (*) 1
3456maximum = foldl1 max
3557minimum = foldl1 min
3658
59+partition : (a -> Bool) -> [a] -> ([a],[a])
3760partition pred lst =
3861 case lst of
39− { x:xs -> case partition pred xs of
40− { (as,bs) -> if pred x then (x:as,bs) else (as,x:bs) }
41− ; [] -> ([],[]) }
62+ [] -> ([],[])
63+ x::xs -> let (as,bs) = partition pred xs in
64+ if pred x then (x::as,bs) else (as,x::bs)
4265
43−zipWith f listA listB = case (listA, listB) of
44− { (a:as, b:bs) -> f a b : zipWith f as bs; _ -> [] }
66+zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
67+zipWith = Native.zipWith
4568
46−zip listA listB = case (listA, listB) of
47− { (a:as, b:bs) -> (a,b) : zip as bs; _ -> [] }
69+zip : [a] -> [b] -> [(a,b)]
70+zip = Native.zip
4871
72+unzip : [(a,b)] -> ([a],[b])
4973unzip pairs =
5074 case pairs of
51− { p:ps -> case (p, unzip ps) of { ((x,y),(xs,ys)) -> (x:xs,y:ys) }
52− ; [] -> ([],[]) }
75+ [] -> ([],[])
76+ (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
5377
5478intersperse sep xs =
55− case xs of { a:b:cs -> a : sep : intersperse sep (b:cs)
56− ; a:[] -> [a] ; [] -> [] }
79+ case xs of
80+ a::b::cs -> a :: sep :: intersperse sep (b::cs)
81+ [a] -> [a]
82+ [] -> []
83+
5784intercalate sep xs =
58− case xs of { a:b:cs -> a ++ sep ++ intercalate sep (b:cs)
59− ; a:[] -> a ; [] -> [] }
85+ case xs of
86+ a::b::cs -> a ++ sep ++ intercalate sep (b::cs)
87+ [a] -> a
88+ [] -> []