Many functions can be generalised

David Menendez dave at zednenem.com
Fri Dec 2 23:14:13 UTC 2016


On Mon, Nov 28, 2016 at 4:41 PM, wren romano <wren at community.haskell.org>
wrote:

> I'd much rather see the above functions as:
>
>     mapMaybes :: Foo f => (a -> Maybe b) -> f a -> f b
>     catMaybes :: Foo f => f (Maybe a) -> f a
>

A while back, I found myself deriving this class:

    class Functor f => Siftable f where
        siftWith :: (a -> Maybe b) -> f a -> f b
        sift :: (a -> Bool) -> f a -> f a
        sift f = siftWith (\a -> if f a then Just a else Nothing)

which is essentially Witherable minus Traversable. It has the nice property
that it’s a functor from the Kleisli category for Maybe to Hask, so the
laws are intuitive and easily expressed. You can even express wither using
siftWith and traverse

    wither :: (Siftable t, Traversable t, Applicative f) => (a -> f (Maybe
b)) -> t a -> f (t b)
    wither f = fmap (siftWith id) . traverse f

But it turns out that there aren’t many instances of Siftable that aren’t
also Traversable. The most obvious would be infinite streams, but even they
have a traversal if you restrict yourself to lazy applicatives.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20161202/152cb44b/attachment.html>


More information about the Libraries mailing list