compactable 0.1.0.2 → 0.1.0.3
raw patch · 2 files changed
+17/−7 lines, 2 files
Files
- compactable.cabal +1/−1
- src/Control/Compactable.hs +16/−6
compactable.cabal view
@@ -1,5 +1,5 @@ name: compactable-version: 0.1.0.2+version: 0.1.0.3 synopsis: A generalization for containers that can be stripped of Nothings. description: Sometimes you have a collection of Maybes, and you want to extract the values. Actually that happens a whole lot.
src/Control/Compactable.hs view
@@ -15,6 +15,7 @@ import Data.Proxy import Data.Semigroup import qualified Data.Sequence as Seq+import qualified Data.Set as Set import Data.Vector (Vector) import GHC.Conc import Text.ParserCombinators.ReadP@@ -134,25 +135,25 @@ traverseMaybe f = fmap compact . traverse f {-# INLINABLE traverseMaybe #-} - filter :: Functor f => (a -> Bool) -> f a -> f a+ filter :: (a -> Bool) -> f a -> f a+ default filter :: Functor f => (a -> Bool) -> f a -> f a filter f = fmapMaybe $ \a -> if f a then Just a else Nothing {-# INLINABLE filter #-} instance Compactable Maybe where compact = join {-# INLINABLE compact #-}- fmapMaybe f (Just x) = f x- fmapMaybe _ _ = Nothing+ fmapMaybe = (=<<) {-# INLINABLE fmapMaybe #-} instance Compactable [] where compact = catMaybes {-# INLINABLE compact #-} fmapMaybe _ [] = []- fmapMaybe f (h:t) =- maybe (fmapMaybe f t) (: fmapMaybe f t) (f h)+ fmapMaybe f (h:t) = maybe (fmapMaybe f t) (: fmapMaybe f t) (f h) {-# INLINABLE fmapMaybe #-} filter = Prelude.filter+ {-# INLINABLE filter #-} instance Compactable IO @@ -171,7 +172,7 @@ instance Compactable ReadPrec -instance ( Compactable f, Compactable g )+instance ( Functor f, Functor g, Compactable f, Compactable g ) => Compactable (FP.Product f g) where compact (FP.Pair x y) = FP.Pair (compact x) (compact y) {-# INLINABLE compact #-}@@ -222,6 +223,15 @@ compact (Right _) = Left mempty compact (Left x) = Left x {-# INLINABLE compact #-}+ fmapMaybe f (Right x) = maybe (Left mempty) Right (f x)+ fmapMaybe _ (Left x) = Left x+ {-# INLINABLE fmapMaybe #-}++instance Compactable Set.Set where+ compact = Set.fromDistinctAscList . compact . Set.toAscList+ {-# INLINABLE compact #-}+ filter f = Set.fromDistinctAscList . Control.Compactable.filter f . Set.toAscList+ {-# INLINABLE filter #-} fforMaybe :: (Compactable f, Functor f) => f a -> (a -> Maybe b) -> f b