Return to Snippet

Revision: 51858
at October 6, 2011 07:53 by deepsoul

Initial Code
-- Prepend an element to a list if available.  Leave the list as it is if the
-- first argument is Nothing.
maybecons :: Maybe t -> [t] -> [t]
maybecons Nothing l = l
maybecons (Just e) l = e : l

-- Variant of map which deletes elements if the map function returns Nothing.
filtermap :: (a -> Maybe b) -> [a] -> [b]
filtermap _ [] = []
filtermap f (a:as) = maybecons (f a) $ filtermap f as

Initial URL

Initial Description
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.

Initial Title
Simultaneous filter and  map

Initial Tags

Initial Language