mapMaybe

The basic mapMaybe function resides in the Data.Maybe module Data.Maybe of the base library, but it is not available in the Prelude module. It is a useful filtering function.

It takes an (a -> Maybe b) function and maps that over a list of a values. The return values is a list of b: any Nothing values from the application of the (a -> Maybe b) function will be filtered out, and the Just constructors will also be removed from the final list. The documentation gives a good example, altered a bit here to use type applications:

@Int is a type application that readMaybe what the return type should be; the “read” functions usually need to be told what type of value they are trying to “read” out of the strings they’re applied to. Notice how it nicely filters out the values that cannot be read as Int values.

While the basic mapMaybe is specialized to lists, there are plenty of other versions of it around, specialized to other types of collections.

And there is another we’d like to highlight, a less specialized version in the witherable package. Data.Witherable

In this package, mapMaybe is one of the two primary methods of the typeclass called Filterable, which is described as being like Functor, but with Maybe effects. In turn, Filterable allows us to have a special kind of “traversable” typeclass called Witherable that allows for traversal of data structures while pruning – or “withering” – them by removing some elements (the Nothings). You’ll find instances here for some of the same types we just listed as having specializations of mapMaybe (Vector, Map, etc.) but here they’re gathered into one typeclass.

Note that the mapMaybe from Data.Witherable and Data.Map are the same for the Map type.

And a few, such as an instance for Either, are available that don’t exist elsewhere.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.