witherable 0.1.2 → 0.1.2.1
raw patch · 2 files changed
+18/−7 lines, 2 files
Files
- src/Data/Witherable.hs +17/−6
- witherable.cabal +1/−1
src/Data/Witherable.hs view
@@ -31,19 +31,19 @@ -- | Like `traverse`, but you can remove elements instead of updating them. -- --- @traverse f ≡ wither (fmap Just . f)@ +-- @'traverse' f ≡ 'wither' ('fmap' 'Just' . f)@ -- -- A definition of 'wither' must satisfy the following laws: -- -- [/identity/] --- @wither (pure . Just) ≡ pure@ +-- @'wither' ('pure' . Just) ≡ 'pure'@ -- -- [/composition/] --- @Compose . fmap (wither f) . wither g ≡ wither (Compose . fmap (maybe (pure Nothing) f) . g)@ +-- @Compose . fmap ('wither' f) . 'wither' g ≡ 'wither' (Compose . fmap ('wither' f) . g)@ -- -- Parametricity implies the naturality law: -- --- @t . wither f = wither (t . f)@ +-- @t . 'wither' f = 'wither' (t . f)@ -- -- Minimal complete definition: `wither` or `catMaybes`. -- The default definitions can be overriden for efficiency. @@ -52,17 +52,24 @@ wither :: Applicative f => (a -> f (Maybe b)) -> t a -> f (t b) wither f = fmap catMaybes . T.traverse f + mapMaybe :: (a -> Maybe b) -> t a -> t b + mapMaybe f = runIdentity . wither (Identity . f) + {-# INLINE mapMaybe #-} + catMaybes :: t (Maybe a) -> t a - catMaybes = runIdentity . wither pure + catMaybes = mapMaybe id + {-# INLINE catMaybes #-} filterA :: Applicative f => (a -> f Bool) -> t a -> f (t a) filterA f = wither (\a -> (\b -> if b then Just a else Nothing) <$> f a) filter :: (a -> Bool) -> t a -> t a filter f = runIdentity . filterA (Identity . f) + {-# INLINE filter #-} witherM :: (Witherable t, Monad m) => (a -> MaybeT m b) -> t a -> m (t b) witherM f = unwrapMonad . wither (WrapMonad . runMaybeT . f) +{-# INLINE witherM #-} -- | 'blightM' is 'witherM' with its arguments flipped. blightM :: (Monad m, Witherable t) => t a -> (a -> MaybeT m b) -> m (t b) @@ -90,12 +97,16 @@ instance Witherable IM.IntMap where wither f = fmap IM.fromAscList . wither (\(i, a) -> fmap ((,) i) <$> f a) . IM.toList {-# INLINABLE wither #-} + mapMaybe = IM.mapMaybe + {-# INLINE mapMaybe #-} filter = IM.filter - {-# INLINABLE filter #-} + {-# INLINE filter #-} instance Ord k => Witherable (M.Map k) where wither f = fmap M.fromAscList . wither (\(i, a) -> fmap ((,) i) <$> f a) . M.toList {-# INLINABLE wither #-} + mapMaybe = M.mapMaybe + {-# INLINE mapMaybe #-} filter = M.filter {-# INLINABLE filter #-}
witherable.cabal view
@@ -1,5 +1,5 @@ name: witherable -version: 0.1.2 +version: 0.1.2.1 synopsis: Generalization of filter and catMaybes -- description: homepage: https://github.com/fumieval/witherable