mapMaybe package:rio

Transform this map by applying a function to every value and retaining only some of them.
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"
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 is Just b, then b is included in the result list.

Examples

Using mapMaybe f x is a shortcut for catMaybes $ map f x in most cases:
>>> import 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]
O(n) Drop elements when predicate returns Nothing
O(n) Drop elements when predicate returns Nothing
O(n) Drop elements when predicate returns Nothing
O(n) Drop elements when predicate returns Nothing
Transform this map by applying a function to every value and retaining only some of them.
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"
Applicative mapMaybe.
Monadic mapMaybe.
A vesion of contramapMaybeGLogFunc which supports filering.
O(n) Drop elements when predicate, applied to index and value, returns Nothing
O(n) Drop elements when predicate, applied to index and value, returns Nothing
O(n) Drop elements when predicate, applied to index and value, returns Nothing
O(n) Drop elements when predicate, applied to index and value, returns Nothing