mapMaybe -package:hedgehog

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 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]
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"
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"
Map values and collect the Just results.
Transform this map by applying a function to every value and retaining only some of them.
Apply a transformation that may fail to all values in a stream, discarding the failures. Subject to fusion Since 0.5.1
Filter values.
>>> toList $ mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..10]) :: [Int]
[1,3,5,7,9]
>>> mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..2]) :: SourceT Identity Int
fromStepT (Effect (Identity (Skip (Yield 1 (Skip Stop)))))
Illustrates why we need Skip.
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"
(mapMaybe f) yields Just results of f. Basic laws:
mapMaybe (f >=> g) = mapMaybe f >-> mapMaybe g

mapMaybe (pure @Maybe . f) = mapMaybe (Just . f) = map f

mapMaybe (const Nothing) = drain
As a result of the second law,
mapMaybe return = mapMaybe Just = cat
Analogous to mapMaybe in Data.Maybe.
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]
A version of map that discards elements mapMaybe f s passes all output from s through the function f and discards elements for which f s evaluates to Nothing. Example:
ghci> Streams.fromList [Just 1, None, Just 3] >>=
Streams.mapMaybe id >>=
Streams.toList
[1,3]
Since: 1.2.1.0
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 Stream. If it is Just b, then b is included in the result Stream.
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) 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 all values in a Map using the supplied function, deleting the key if the function returns Nothing
>>> mapMaybe Data.Maybe.listToMaybe (fromList [("C",[1]),("B",[]),("A",[3])])
fromList [("C",1),("A",3)]
Treats a finite map like an infinite map, where all undefined elements are Nothing and defined elements are Just.
Like mapMaybe.
O(n). Map values and collect the Just results.