@@ -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
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
15# Taking Lists Apart
15# Taking Lists Apart
16@docs partition, unzip
16@docs partition, unzip
19−@docs foldr, foldl, foldr1, foldl1, scanl, scanl1
19+@docs foldr, foldl
22+@docs filterMap, concatMap, indexedMap
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
25@docs sort, sortBy, sortWith
28@docs sort, sortBy, sortWith
@@ -35,6 +38,7 @@ possibly should be approached another way.
37import Basics (..)
40import Basics (..)
41+import Maybe ( Maybe(Just,Nothing) )
38import Native.List
42import Native.List
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
88+{-| Same as `map` but the function is also applied to the index of each
89+element (starting at zero).
91+ indexedMap (,) ["Tom","Sue","Bob"] == [ (0,"Tom"), (1,"Sue"), (2,"Bob") ]
93+indexedMap : (Int -> a -> b) -> [a] -> [b]
95+ zipWith f [ 0 .. length xs - 1 ] xs
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
133+{-| Apply a function that may succeed to all values in the list, but only keep
136+ toInt : String -> Maybe Int
138+ filterMap toInt ["3", "4.0", "5", "hats"] == [3,5]
140+filterMap : (a -> Maybe b) -> [a] -> [b]
141+filterMap xs = Native.List.foldr maybeCons [] xs
143+maybeCons : Maybe a -> [a] -> [a]
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
136−{-| Check to see if all elements are True. -}
138−and = foldl (&&) True
140−{-| Check to see if any elements are True. -}
142−or = foldl (||) False
144{-| Concatenate a list of appendable things:
165{-| Concatenate a list of appendable things:
146 concat [[1,2],[3],[4,5]] == [1,2,3,4,5]
167 concat [[1,2],[3],[4,5]] == [1,2,3,4,5]