Many functions can be generalised

David Feuer david.feuer at gmail.com
Sat Dec 3 05:50:27 UTC 2016


On Dec 2, 2016 6:14 PM, "David Menendez" <dave at zednenem.com> wrote:

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)


I would expect several classes, corresponding to different methods of
Witherable:

class Siftable a m | m -> a where
  sift :: (a -> Bool) -> m -> m
  default sift :: SiftWithable f => (a -> Bool) -> f a -> f a
  sift p = siftWith (\x -> x <$ guard (p x))

class Functor f => SiftWithable f where
  siftWith :: (a -> Maybe b) -> f a -> f b

class Siftable a m => SiftableA a m where
  siftA :: Applicative g => (a -> g Bool) -> m -> g m
  default siftA :: (SiftWithAAble f, Applicative g) => (a -> g Bool) -> f a
-> g (f a)
  siftA p = siftWithA (\x -> (x <$) . guard <$> p x)

class (Traversable f, SiftWithAble f) => SiftWithAAble f where
  siftWithA :: Applicative g => (a -> g (Maybe b)) -> f a -> g (f a)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20161203/d9c8a62a/attachment.html>


More information about the Libraries mailing list