[Haskell-cafe] Generalizing catMaybes

David Menendez dave at zednenem.com
Sat Jan 8 05:27:39 CET 2011


On Fri, Jan 7, 2011 at 9:56 PM, Tony Morris <tonymorris at gmail.com> wrote:
>
>  I am wondering if it possible to generalise catMaybes:
>
> (Something f, SomethingElse t) => t (f a) -> t a
>
> I have being doing some gymnastics with Traversable and Foldable and a
> couple of other things from category-extras to no avail. Perhaps
> someone else's brain is molded into an appropriate shape to reveal an
> answer!


This gets you part of the way there:

(>>= maybe mzero return) :: (MonadPlus m) => m (Maybe a) -> m a

I'm not sure what the SomethingElse should look like. One possibility
would be Foldable.

(>>= Data.Foldable.foldr (mplus . return) mzero)
  :: (MonadPlus m, Foldable t) => m (t a) -> m a



I have a MonadPlus-to-Monoid wrapper I call MSum, so I could also write it,

(>>= getMSum . foldMap (MSum . return))
  :: (MonadPlus m, Foldable t) => m (t a) -> m a


-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>



More information about the Haskell-Cafe mailing list