
On Mon, Nov 28, 2016 at 4:41 PM, wren romano
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