on master help sign in
Update the List library to use the number, comparable, and appendable type variables
commit067773d601859a717941e09f62766fe72ef1eb23 authorEvan Czaplicki <info@elm-lang.org> dateThu 25 Jul 2013, 16:40 (13 years ago) parentd059c36
2 files changed+57−26
libraries/List.elm+50−25 libraries/Native/List.js+7−1
libraries/List.elm+50−25
@@ -1,29 +1,34 @@
11
22module List where
33
4−import Native.Utils (min, max)
5−import Native.List as L
4+import Native.Utils
5+import Native.List as Native
66
77-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
88(::) : a -> [a] -> [a]
9+(::) = Native.cons
910
1011-- Puts two appendable things together:
1112--
1213-- [1,1] ++ [2,3] == [1,1,2,3]
1314-- "abc" ++ "123" == "abc123"
14−(++) : Appendable a -> Appendable a -> Appendable a
15+(++) : appendable -> appendable -> appendable
16+(++) = Native.append
1517
1618-- Extract the first element of a list. List must be non-empty.
1719-- `(head [1,2,3] == 1)`
1820head : [a] -> a
21+head = Native.head
1922
2023-- Extract the elements after the head of the list. List must be non-empty.
2124-- `(tail [1,2,3] == [2,3])`
2225tail : [a] -> [a]
26+tail = Native.tail
2327
2428-- Extract the last element of a list. List must be non-empty.
2529-- `(last [1,2,3] == 3)`
2630last : [a] -> a
31+last = Native.last
2732
2833-- Check if a list is empty `(isEmpty [] == True)`
2934isEmpty : [a] -> Bool
@@ -33,77 +38,92 @@ isEmpty xs =
3338 _ -> False
3439
3540-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
36−map : (a -> b) -> [a] -> [b]
41+map : (a -> b) -> [a] -> [b]
42+map = Native.map
3743
3844-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
39−foldl : (a -> b -> b) -> b -> [a] -> b
45+foldl : (a -> b -> b) -> b -> [a] -> b
46+foldl = Native.foldl
4047
4148-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
42−foldr : (a -> b -> b) -> b -> [a] -> b
49+foldr : (a -> b -> b) -> b -> [a] -> b
50+foldr = Native.foldr
4351
4452-- Reduce a list from the left without a base case. List must be non-empty.
4553foldl1 : (a -> a -> a) -> [a] -> a
54+foldl1 = Native.foldl1
4655
4756-- Reduce a list from the right without a base case. List must be non-empty.
4857foldr1 : (a -> a -> a) -> [a] -> a
58+foldr1 = Native.foldr1
4959
5060-- Reduce a list from the left, building up all of the intermediate results into a list.
5161--
5262-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
53−scanl : (a -> b -> b) -> b -> [a] -> [b]
63+scanl : (a -> b -> b) -> b -> [a] -> [b]
64+scanl = Native.scanl
5465
5566-- Same as scanl but it doesn't require a base case. List must be non-empty.
5667--
5768-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
5869scanl1 : (a -> a -> a) -> [a] -> [a]
70+scanl1 = Native.scanl1
5971
6072-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
61−filter : (a -> Bool) -> [a] -> [a]
73+filter : (a -> Bool) -> [a] -> [a]
74+filter = Native.filter
6275
6376-- Determine the length of a list: `(length "innumerable" == 11)`
64−length : [a] -> Int
77+length : [a] -> Int
78+length = Native.length
6579
6680-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
6781reverse : [a] -> [a]
82+reverse = Native.reverse
6883
6984-- Check to see if all elements satisfy the predicate.
7085all : (a -> Bool) -> [a] -> Bool
86+all = Native.all
7187
7288-- Check to see if any elements satisfy the predicate.
7389any : (a -> Bool) -> [a] -> Bool
90+any = Native.any
7491
7592-- Check to see if all elements are True.
7693and : [Bool] -> Bool
94+and = Native.and
7795
7896-- Check to see if any elements are True.
79−or : [Bool] -> Bool
97+or : [Bool] -> Bool
98+or = Native.or
8099
81100-- Concatenate a list of appendable things:
82101--
83102-- concat ["tree","house"] == "treehouse"
84−concat : [Appendable a] -> Appendable a
103+concat : [appendable] -> appendable
104+concat = Native.concat
85105
86106-- Map a given function onto a list and flatten the resulting lists.
87107--
88108-- concatMap f xs == concat (map f xs)
89−concatMap : (a -> Appendable b) -> [a] -> Appendable b
90−concatMap f = L.concat . L.map f
109+concatMap : (a -> appendable) -> [a] -> appendable
110+concatMap f list = concat (map f list)
91111
92112-- Get the sum of the list elements. `(sum [1..4] == 10)`
93−sum : [Number a] -> Number a
94−sum = L.foldl (+) 0
113+sum : [number] -> number
114+sum = foldl (+) 0
95115
96116-- Get the product of the list elements. `(product [1..4] == 24)`
97−product : [Number a] -> Number a
98−product = L.foldl (*) 1
117+product : [number] -> number
118+product = foldl (*) 1
99119
100−-- Find the highest number in a non-empty list.
101−maximum : [Number a] -> Number a
102−maximum = L.foldl1 max
120+-- Find the maximum element in a non-empty list: `maximum [1,4,2] == 4`
121+maximum : [comparable] -> comparable
122+maximum = foldl1 Native.Utils.max
103123
104−-- Find the lowest number in a non-empty list.
105−minimum : [Number a] -> Number a
106−minimum = L.foldl1 min
124+-- Find the minimum element in a non-empty list: `minimum [3,2,1] == 1`
125+minimum : [comparable] -> comparable
126+minimum = foldl1 Native.Utils.min
107127
108128-- Split a list based on the predicate.
109129partition : (a -> Bool) -> [a] -> ([a],[a])
@@ -119,12 +139,14 @@ partition pred lst =
119139-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
120140-- zip == zipWith (,)
121141zip : [a] -> [b] -> [(a,b)]
142+zip = Native.zip
122143
123144-- Combine two lists, combining them with the given function.
124145-- If one list is longer, the extra elements are dropped.
125146--
126147-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
127148zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
149+zipWith = Native.zipWith
128150
129151-- Decompose a list of tuples.
130152unzip : [(a,b)] -> ([a],[b])
@@ -137,12 +159,14 @@ unzip pairs =
137159--
138160-- split "," "hello,there,friend" == ["hello", "there", "friend"]
139161split : [a] -> [a] -> [[a]]
162+split = Native.split
140163
141164-- Places the given value between all of the lists in the second argument
142165-- and concatenates the result.
143166--
144167-- join "a" ["H","w","ii","n"] == "Hawaiian"
145−join : Appendable a -> [Appendable a] -> Appendable a
168+join : appendable -> [appendable] -> appendable
169+join = Native.join
146170
147171-- Places the given value between all members of the given list.
148172--
@@ -156,7 +180,8 @@ intersperse sep xs =
156180
157181-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
158182take : Int -> [a] -> [a]
183+take = Native.take
159184
160185-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
161186drop : Int -> [a] -> [a]
162−
187+drop = Native.drop
libraries/Native/List.js+7−1
@@ -90,7 +90,7 @@ Elm.Native.List = function(elm) {
9090 }
9191
9292 // f defined similarly for both foldl and foldr (NB: different from Haskell)
93− // ie, foldl :: (a -> b -> b) -> b -> [a] -> b
93+ // ie, foldl : (a -> b -> b) -> b -> [a] -> b
9494 function foldl(f, b, xs) {
9595 var acc = b;
9696 while (xs.ctor !== '[]') {
@@ -219,6 +219,10 @@ Elm.Native.List = function(elm) {
219219 return fromArray(toArray(xs).sort(cmp));
220220 }
221221
222+ function nth(xs, n) {
223+ return toArray(xs)[n];
224+ }
225+
222226 function take(n, xs) {
223227 var arr = [];
224228 while (xs.ctor !== '[]' && n > 0) {
@@ -308,6 +312,7 @@ Elm.Native.List = function(elm) {
308312 Elm.Native.List.values = {
309313 Nil:Nil,
310314 Cons:Cons,
315+ cons:F2(Cons),
311316 toArray:toArray,
312317 fromArray:fromArray,
313318 range:range,
@@ -336,6 +341,7 @@ Elm.Native.List = function(elm) {
336341 zipWith:F3(zipWith),
337342 zip:F2(zip),
338343 sort:sort,
344+ nth:F2(nth),
339345 take:F2(take),
340346 drop:F2(drop),
341347