on master help sign in
Augment the List library
de6053bEvan Czaplicki
changelog.md+7
@@ -6,6 +6,8 @@
6 * Type aliases in port types
6 * Type aliases in port types
7 * Add Keyboard.alt and Keyboard.meta
7 * Add Keyboard.alt and Keyboard.meta
8 * Add Debug.crash
8 * Add Debug.crash
9+ * Add List.indexedMap and List.filterMap
10+ * Add Maybe.map
9 * Add --bundle-runtime flag which creates stand-alone Elm programs
11 * Add --bundle-runtime flag which creates stand-alone Elm programs
10 * Error on ambiguious use of imported variables
12 * Error on ambiguious use of imported variables
11 * Better architecture for compiler. Uses types to make compilation pipeline
13 * Better architecture for compiler. Uses types to make compilation pipeline
@@ -14,6 +16,11 @@
14
16
15#### Breaking Changes:
17#### Breaking Changes:
16
18
19+ * Rename Maybe.maybe to Maybe.extract
20+ * Rename Either.either to Either.extract
21+ * Remove Maybe.justs for (List.filterMap id)
22+ * Remove List.and for (List.foldl (&&) True)
23+ * Remove List.or for (List.foldl (||) False)
17 * Unambiguous syntax for importing ADTs and type aliases (whoo!)
24 * Unambiguous syntax for importing ADTs and type aliases (whoo!)
18 * sqrt and logBase both only work on Floats now
25 * sqrt and logBase both only work on Floats now
19
26
libraries/List.elm+32−11
@@ -10,16 +10,19 @@ list must have the same type.
10@docs head, tail, last, filter, take, drop
10@docs head, tail, last, filter, take, drop
11
11
12# Putting Lists Together
12# Putting Lists Together
13−@docs concat, concatMap, join, intersperse, zip, zipWith, repeat
13+@docs concat, join, intersperse, zip, zipWith, repeat
14
14
15# Taking Lists Apart
15# Taking Lists Apart
16@docs partition, unzip
16@docs partition, unzip
17
17
18# Folds
18# Folds
19−@docs foldr, foldl, foldr1, foldl1, scanl, scanl1
19+@docs foldr, foldl
20+
21+# Special Maps
22+@docs filterMap, concatMap, indexedMap
20
23
21# Special Folds
24# Special Folds
22−@docs sum, product, maximum, minimum, all, any, and, or
25+@docs sum, product, maximum, minimum, all, any, foldr1, foldl1, scanl, scanl1
23
26
24# Sorting
27# Sorting
25@docs sort, sortBy, sortWith
28@docs sort, sortBy, sortWith
@@ -35,6 +38,7 @@ possibly should be approached another way.
35-}
38-}
36
39
37import Basics (..)
40import Basics (..)
41+import Maybe ( Maybe(Just,Nothing) )
38import Native.List
42import Native.List
39
43
40{-| Add an element to the front of a list `(1 :: [2,3] == [1,2,3])` -}
44{-| Add an element to the front of a list `(1 :: [2,3] == [1,2,3])` -}
@@ -81,6 +85,15 @@ isEmpty xs =
81map : (a -> b) -> [a] -> [b]
85map : (a -> b) -> [a] -> [b]
82map = Native.List.map
86map = Native.List.map
83
87
88+{-| Same as `map` but the function is also applied to the index of each
89+element (starting at zero).
90+
91+ indexedMap (,) ["Tom","Sue","Bob"] == [ (0,"Tom"), (1,"Sue"), (2,"Bob") ]
92+-}
93+indexedMap : (Int -> a -> b) -> [a] -> [b]
94+indexedMap f xs =
95+ zipWith f [ 0 .. length xs - 1 ] xs
96+
84{-| Reduce a list from the left: `(foldl (::) [] [1,2,3] == [3,2,1])` -}
97{-| Reduce a list from the left: `(foldl (::) [] [1,2,3] == [3,2,1])` -}
85foldl : (a -> b -> b) -> b -> [a] -> b
98foldl : (a -> b -> b) -> b -> [a] -> b
86foldl = Native.List.foldl
99foldl = Native.List.foldl
@@ -117,6 +130,22 @@ scanl1 = Native.List.scanl1
117filter : (a -> Bool) -> [a] -> [a]
130filter : (a -> Bool) -> [a] -> [a]
118filter = Native.List.filter
131filter = Native.List.filter
119
132
133+{-| Apply a function that may succeed to all values in the list, but only keep
134+the successes.
135+
136+ toInt : String -> Maybe Int
137+
138+ filterMap toInt ["3", "4.0", "5", "hats"] == [3,5]
139+-}
140+filterMap : (a -> Maybe b) -> [a] -> [b]
141+filterMap xs = Native.List.foldr maybeCons [] xs
142+
143+maybeCons : Maybe a -> [a] -> [a]
144+maybeCons mx xs =
145+ case mx of
146+ Just x -> x :: xs
147+ Nothing -> xs
148+
120{-| Determine the length of a list: `(length [1,2,3] == 3)` -}
149{-| Determine the length of a list: `(length [1,2,3] == 3)` -}
121length : [a] -> Int
150length : [a] -> Int
122length = Native.List.length
151length = Native.List.length
@@ -133,14 +162,6 @@ all = Native.List.all
133any : (a -> Bool) -> [a] -> Bool
162any : (a -> Bool) -> [a] -> Bool
134any = Native.List.any
163any = Native.List.any
135
164
136−{-| Check to see if all elements are True. -}
137−and : [Bool] -> Bool
138−and = foldl (&&) True
139−
140−{-| Check to see if any elements are True. -}
141−or : [Bool] -> Bool
142−or = foldl (||) False
143−
144{-| Concatenate a list of appendable things:
165{-| Concatenate a list of appendable things:
145
166
146 concat [[1,2],[3],[4,5]] == [1,2,3,4,5]
167 concat [[1,2],[3],[4,5]] == [1,2,3,4,5]
libraries/Maybe.elm+1−14
@@ -1,4 +1,4 @@
1−module Maybe (Maybe(..), maybe, isJust, isNothing, justs) where
1+module Maybe (Maybe(..), maybe, isJust, isNothing) where
2
2
3{-| Represents an optional value. Maybe it is there, maybe it is not.
3{-| Represents an optional value. Maybe it is there, maybe it is not.
4
4
@@ -8,12 +8,9 @@ module Maybe (Maybe(..), maybe, isJust, isNothing, justs) where
8# Taking Maybes apart
8# Taking Maybes apart
9@docs maybe, isJust, isNothing
9@docs maybe, isJust, isNothing
10
10
11−# Maybes and Lists
12−@docs justs
13-}
11-}
14
12
15import Basics (not, (.))
13import Basics (not, (.))
16−import List (foldr, (::))
17
14
18{-| The Maybe datatype. Useful when a computation may or may not
15{-| The Maybe datatype. Useful when a computation may or may not
19result in a value (e.g. logarithm is defined only for positive
16result in a value (e.g. logarithm is defined only for positive
@@ -53,13 +50,3 @@ isJust = maybe False (\_ -> True)
53-}
50-}
54isNothing : Maybe a -> Bool
51isNothing : Maybe a -> Bool
55isNothing = not . isJust
52isNothing = not . isJust
56−
57−cons : Maybe a -> [a] -> [a]
58−cons mx xs = maybe xs (\x -> x :: xs) mx
59−
60−{-| Filters out Nothings and extracts the remaining values.
61−
62− justs [Just 0, Nothing, Just 5, Just 7] == [0,5,7]
63−-}
64−justs : [Maybe a] -> [a]
65−justs = foldr cons []