[Haskell-cafe] Generalizing catMaybes

Henning Thielemann lemming at henning-thielemann.de
Sat Jan 8 18:42:35 CET 2011


On Sat, 8 Jan 2011, Conor McBride wrote:

> Of course, there is an alternative generalisation.
>
> [] and Maybe are both Foldable, hence so is their composition.
>
> There's got to be a thing of type
>
> collapse :: (Foldable f, Alternative a) => f x -> a x
>
> which would do the job.

Nice! It would be

  collapse = Data.Foldable.foldr (\a b -> pure a <|> b) empty

and with transformers:Data.Functor.Compose (or the one from TypeCompose) 
we get

  catMaybes = collapse . Compose


> Of course, anything which is both foldable and alternative
> certainly has a function with the type of join.

  join = collapse . Compose

:-)



More information about the Haskell-Cafe mailing list