On Sat, Dec 3, 2016 at 12:50 AM, David Feuer <david.feuer@gmail.com> wrote:On Dec 2, 2016 6:14 PM, "David Menendez" <dave@zednenem.com> wrote:A while back, I found myself deriving this class:class Functor f => Siftable f wheresiftWith :: (a -> Maybe b) -> f a -> f bsift :: (a -> Bool) -> f a -> f asift 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 wheresift :: (a -> Bool) -> m -> mdefault sift :: SiftWithable f => (a -> Bool) -> f a -> f asift p = siftWith (\x -> x <$ guard (p x))class Functor f => SiftWithable f wheresiftWith :: (a -> Maybe b) -> f a -> f bclass Siftable a m => SiftableA a m wheresiftA :: Applicative g => (a -> g Bool) -> m -> g mdefault 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 wheresiftWithA :: Applicative g => (a -> g (Maybe b)) -> f a -> g (f a)Yes, sift is more general than siftWith (which I should have called siftMap, in hindsight). But, so far as I know, the only things you can define sift for but not siftWith are sets and set-like things.At the time, I had also rejected sift by itself because I couldn’t think of any laws, but now that I look at it again, I guess they would be:sift (const True) = idsift (\x -> p x && q x) = sift q . sift pI think those would make sift a monoid homomorphism.These still allow some weird instances, like sift _ = id, or something like this:newtype Weird a = Map a Boolinstance Ord a => Siftable a (Weird a) wheresift p (Weird m) = Weird (Map.union (Map.updateMin (const False) yes) no)where(yes, no) = Map.partitionWithKey (const . p) mI imagine it isn’t worth making the laws tighter to forbid this.--