/ Published in: Haskell
The `filtermap` functional below allows to perform the standard `map` and `filter` operations in one go. The mapping function has to return a `Maybe` monad, and values of `Nothing` are filtered out.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- Prepend an element to a list if available. Leave the list as it is if the -- first argument is Nothing. maybecons Nothing l = l maybecons (Just e) l = e : l -- Variant of map which deletes elements if the map function returns Nothing. filtermap _ [] = [] filtermap f (a:as) = maybecons (f a) $ filtermap f as