on master help sign in
Augment the List library

add filterMap and indexedMap remove and/or special folds remove Maybe.justs to remove circular dependency

commitde6053b036233e0cba5d1aafec0114d08e5df032 authorEvan Czaplicki <info@elm-lang.org> dateWed 23 Jul 2014, 16:51 (12 years ago) parent066cfff
3 files changed+40−25
changelog.md+7 libraries/List.elm+32−11 libraries/Maybe.elm+1−14
changelog.md+7
@@ -6,6 +6,8 @@
66 * Type aliases in port types
77 * Add Keyboard.alt and Keyboard.meta
88 * Add Debug.crash
9+ * Add List.indexedMap and List.filterMap
10+ * Add Maybe.map
911 * Add --bundle-runtime flag which creates stand-alone Elm programs
1012 * Error on ambiguious use of imported variables
1113 * Better architecture for compiler. Uses types to make compilation pipeline
@@ -14,6 +16,11 @@
1416
1517#### Breaking Changes:
1618
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)
1724 * Unambiguous syntax for importing ADTs and type aliases (whoo!)
1825 * sqrt and logBase both only work on Floats now
1926
libraries/List.elm+32−11
@@ -10,16 +10,19 @@ list must have the same type.
1010@docs head, tail, last, filter, take, drop
1111
1212# Putting Lists Together
13−@docs concat, concatMap, join, intersperse, zip, zipWith, repeat
13+@docs concat, join, intersperse, zip, zipWith, repeat
1414
1515# Taking Lists Apart
1616@docs partition, unzip
1717
1818# Folds
19−@docs foldr, foldl, foldr1, foldl1, scanl, scanl1
19+@docs foldr, foldl
20+
21+# Special Maps
22+@docs filterMap, concatMap, indexedMap
2023
2124# Special Folds
22−@docs sum, product, maximum, minimum, all, any, and, or
25+@docs sum, product, maximum, minimum, all, any, foldr1, foldl1, scanl, scanl1
2326
2427# Sorting
2528@docs sort, sortBy, sortWith
@@ -35,6 +38,7 @@ possibly should be approached another way.
3538-}
3639
3740import Basics (..)
41+import Maybe ( Maybe(Just,Nothing) )
3842import Native.List
3943
4044{-| Add an element to the front of a list `(1 :: [2,3] == [1,2,3])` -}
@@ -81,6 +85,15 @@ isEmpty xs =
8185map : (a -> b) -> [a] -> [b]
8286map = Native.List.map
8387
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+
8497{-| Reduce a list from the left: `(foldl (::) [] [1,2,3] == [3,2,1])` -}
8598foldl : (a -> b -> b) -> b -> [a] -> b
8699foldl = Native.List.foldl
@@ -117,6 +130,22 @@ scanl1 = Native.List.scanl1
117130filter : (a -> Bool) -> [a] -> [a]
118131filter = Native.List.filter
119132
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+
120149{-| Determine the length of a list: `(length [1,2,3] == 3)` -}
121150length : [a] -> Int
122151length = Native.List.length
@@ -133,14 +162,6 @@ all = Native.List.all
133162any : (a -> Bool) -> [a] -> Bool
134163any = Native.List.any
135164
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−
144165{-| Concatenate a list of appendable things:
145166
146167 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
22
33{-| Represents an optional value. Maybe it is there, maybe it is not.
44
@@ -8,12 +8,9 @@ module Maybe (Maybe(..), maybe, isJust, isNothing, justs) where
88# Taking Maybes apart
99@docs maybe, isJust, isNothing
1010
11−# Maybes and Lists
12−@docs justs
1311-}
1412
1513import Basics (not, (.))
16−import List (foldr, (::))
1714
1815{-| The Maybe datatype. Useful when a computation may or may not
1916result in a value (e.g. logarithm is defined only for positive
@@ -53,13 +50,3 @@ isJust = maybe False (\_ -> True)
5350-}
5451isNothing : Maybe a -> Bool
5552isNothing = 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 []