mapMaybe -transformers
The mapMaybe function is a version of map which can throw out elements. In particular, the functional argument returns something of type Maybe b. If this is Nothing, no element is added on to the result list. If it just Just b, then b is included in the result list.
O(n). Map values and collect the Just results.
> let f x = if x == "a" then Just "new a" else Nothing
> mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
O(n). Map values and collect the Just results.
> let f x = if x == "a" then Just "new a" else Nothing
> mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
O(n). Map keys/values and collect the Just results.
> let f k _ = if k < 5 then Just ("key (:) " ++ (show k)) else Nothing
> mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key (:) 3"
O(n). Map keys/values and collect the Just results.
> let f k _ = if k < 5 then Just ("key (:) " ++ (show k)) else Nothing
> mapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key (:) 3"