map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] map f [x1, x2, ...] == [f x1, f x2, ...]this means that map id == id
>>> map (+1) [1, 2, 3] [2,3,4]
>>> map id [1, 2, 3] [1,2,3]
>>> map (\n -> 3 * n + 1) [1, 2, 3] [4,7,10]
>>> mapAccumL (\a b -> (a + b, a)) 0 [1..10] (55,[0,1,3,6,10,15,21,28,36,45])
>>> mapAccumL (\a b -> (a <> show b, a)) "0" [1..5]
("012345",["0","01","012","0123","01234"])
>>> mapAccumR (\a b -> (a + b, a)) 0 [1..10] (55,[54,52,49,45,40,34,27,19,10,0])
>>> mapAccumR (\a b -> (a <> show b, a)) "0" [1..5]
("054321",["05432","0543","054","05","0"])
>>> import GHC.Internal.Text.Read ( readMaybe ) >>> let readMaybeInt = readMaybe :: String -> Maybe Int >>> mapMaybe readMaybeInt ["1", "Foo", "3"] [1,3] >>> catMaybes $ map readMaybeInt ["1", "Foo", "3"] [1,3]If we map the Just constructor, the entire list should be returned:
>>> mapMaybe Just [1,2,3] [1,2,3]
>>> let expensiveDouble a = putStrLn ("Doubling " <> show a) >> pure (2 * a)
>>> :{
mapAccumM (\cache a -> case lookup a cache of
Nothing -> expensiveDouble a >>= \double -> pure ((a, double):cache, double)
Just double -> pure (cache, double)
) [] [1, 2, 3, 1, 2, 3]
:}
Doubling 1
Doubling 2
Doubling 3
([(3,6),(2,4),(1,2)],[2,4,6,2,4,6])
>>> ["1","2","3"] ^. mapping _Show :: [Int] [1,2,3] >>> ([1,2,3] :: [Int]) ^. from (mapping _Show) ["1","2","3"]This also hold across different functors:
>>> let l = mapping @[] @Maybe _Show >>> :t l l :: (Read b, Show b) => Iso [String] (Maybe String) [b] (Maybe b) >>> ["1","2","3"] & l %~ Just . sum Just "6"
pages <- mapConcurrently getURL ["url1", "url2", "url3"]If you just have a list of actions, run them concurrently with
results <- mapConcurrently id [act1, act2, act3]NOTE: mapConcurrently will immediately spawn a thread for each element of the Traversable, so running this on large inputs can lead to resource exhaustion (of memory, file descriptors, or other limited resources). To avoid unbounded resource usage, see Control.Concurrent.Stream.