Data.Filtrable

filtrable-0.1.6.0: Class of filtrable containers

Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Filtrable

Description

See `Filtrable`.

Synopsis

- class Functor f => Filtrable (f :: Type -> Type) where
  - mapMaybe :: (a -> Maybe b) -> f a -> f b
  - catMaybes :: f (Maybe a) -> f a
  - filter :: (a -> Bool) -> f a -> f a
  - mapMaybeA :: (Traversable f, Applicative p) => (a -> p (Maybe b)) -> f a -> p (f b)
  - filterA :: (Traversable f, Applicative p) => (a -> p Bool) -> f a -> p (f a)
  - mapEither :: (a -> Either b c) -> f a -> (f b, f c)
  - mapEitherA :: (Traversable f, Applicative p) => (a -> p (Either b c)) -> f a -> p (f b, f c)
  - partitionEithers :: f (Either a b) -> (f a, f b)
- (<$?>) :: Filtrable f => (a -> Maybe b) -> f a -> f b
- (<*?>) :: (Applicative p, Filtrable p) => p (a -> Maybe b) -> p a -> p b
- nub :: (Filtrable f, Traversable f, Eq a) => f a -> f a
- nubBy :: (Filtrable f, Traversable f) => (a -> a -> Bool) -> f a -> f a
- nubOrd :: (Filtrable f, Traversable f, Ord a) => f a -> f a
- nubOrdBy :: (Filtrable f, Traversable f) => (a -> a -> Ordering) -> f a -> f a

# Documentation

class Functor f => Filtrable (f :: Type -> Type) where #

Class of filtrable containers, i.e. containers we can map over while selectively dropping elements.

Laws:

-     mapMaybe Just = id

-     mapMaybe f = catMaybes ∘ fmap f

-     catMaybes = mapMaybe id

-     filter f = mapMaybe (\ x -> bool Nothing (Just x) (f x))

-     mapMaybe g . mapMaybe f = mapMaybe (g <=< f)

Laws if ``Foldable` f`:

-     foldMap g . filter f = foldMap (\ x -> bool mempty (g x) (f x))

Minimal complete definition

mapMaybe | catMaybes

Methods

mapMaybe :: (a -> Maybe b) -> f a -> f b #

Map the container with the given function, dropping the elements for which it returns `Nothing`.

catMaybes :: f (Maybe a) -> f a #

    catMaybes = mapMaybe id

filter :: (a -> Bool) -> f a -> f a #

Drop the elements for which the given predicate is `False`.

mapMaybeA :: (Traversable f, Applicative p) => (a -> p (Maybe b)) -> f a -> p (f b) #

Traverse the container with the given function, dropping the elements for which it returns `Nothing`.

filterA :: (Traversable f, Applicative p) => (a -> p Bool) -> f a -> p (f a) #

Drop the elements for which the given predicate is `False`.

mapEither :: (a -> Either b c) -> f a -> (f b, f c) #

Map the container with the given function, collecting the `Left`s and the `Right`s separately.

mapEitherA :: (Traversable f, Applicative p) => (a -> p (Either b c)) -> f a -> p (f b, f c) #

Traverse the container with the given function, collecting the `Left`s and the `Right`s separately.

partitionEithers :: f (Either a b) -> (f a, f b) #

    partitionEithers = mapEither id

#### Instances

Instances details

Filtrable Maybe # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Maybe a -> Maybe b # catMaybes :: Maybe (Maybe a) -> Maybe a # filter :: (a -> Bool) -> Maybe a -> Maybe a # mapMaybeA :: (Traversable Maybe, Applicative p) => (a -> p (Maybe b)) -> Maybe a -> p (Maybe b) # filterA :: (Traversable Maybe, Applicative p) => (a -> p Bool) -> Maybe a -> p (Maybe a) # mapEither :: (a -> Either b c) -> Maybe a -> (Maybe b, Maybe c) # mapEitherA :: (Traversable Maybe, Applicative p) => (a -> p (Either b c)) -> Maybe a -> p (Maybe b, Maybe c) # partitionEithers :: Maybe (Either a b) -> (Maybe a, Maybe b) #
Filtrable [] # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> [a] -> [b] # catMaybes :: [Maybe a] -> [a] # filter :: (a -> Bool) -> [a] -> [a] # mapMaybeA :: (Traversable [], Applicative p) => (a -> p (Maybe b)) -> [a] -> p [b] # filterA :: (Traversable [], Applicative p) => (a -> p Bool) -> [a] -> p [a] # mapEither :: (a -> Either b c) -> [a] -> ([b], [c]) # mapEitherA :: (Traversable [], Applicative p) => (a -> p (Either b c)) -> [a] -> p ([b], [c]) # partitionEithers :: [Either a b] -> ([a], [b]) #
Filtrable (Proxy :: Type -> Type) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Proxy a -> Proxy b # catMaybes :: Proxy (Maybe a) -> Proxy a # filter :: (a -> Bool) -> Proxy a -> Proxy a # mapMaybeA :: (Traversable (Proxy :: Type -> Type), Applicative p) => (a -> p (Maybe b)) -> Proxy a -> p (Proxy b) # filterA :: (Traversable (Proxy :: Type -> Type), Applicative p) => (a -> p Bool) -> Proxy a -> p (Proxy a) # mapEither :: (a -> Either b c) -> Proxy a -> (Proxy b, Proxy c) # mapEitherA :: (Traversable (Proxy :: Type -> Type), Applicative p) => (a -> p (Either b c)) -> Proxy a -> p (Proxy b, Proxy c) # partitionEithers :: Proxy (Either a b) -> (Proxy a, Proxy b) #
Filtrable (Const a :: Type -> Type) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a0 -> Maybe b) -> Const a a0 -> Const a b # catMaybes :: Const a (Maybe a0) -> Const a a0 # filter :: (a0 -> Bool) -> Const a a0 -> Const a a0 # mapMaybeA :: (Traversable (Const a :: Type -> Type), Applicative p) => (a0 -> p (Maybe b)) -> Const a a0 -> p (Const a b) # filterA :: (Traversable (Const a :: Type -> Type), Applicative p) => (a0 -> p Bool) -> Const a a0 -> p (Const a a0) # mapEither :: (a0 -> Either b c) -> Const a a0 -> (Const a b, Const a c) # mapEitherA :: (Traversable (Const a :: Type -> Type), Applicative p) => (a0 -> p (Either b c)) -> Const a a0 -> p (Const a b, Const a c) # partitionEithers :: Const a (Either a0 b) -> (Const a a0, Const a b) #
Filtrable f => Filtrable (Backwards f) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Backwards f a -> Backwards f b # catMaybes :: Backwards f (Maybe a) -> Backwards f a # filter :: (a -> Bool) -> Backwards f a -> Backwards f a # mapMaybeA :: (Traversable (Backwards f), Applicative p) => (a -> p (Maybe b)) -> Backwards f a -> p (Backwards f b) # filterA :: (Traversable (Backwards f), Applicative p) => (a -> p Bool) -> Backwards f a -> p (Backwards f a) # mapEither :: (a -> Either b c) -> Backwards f a -> (Backwards f b, Backwards f c) # mapEitherA :: (Traversable (Backwards f), Applicative p) => (a -> p (Either b c)) -> Backwards f a -> p (Backwards f b, Backwards f c) # partitionEithers :: Backwards f (Either a b) -> (Backwards f a, Backwards f b) #
Filtrable f => Filtrable (Reverse f) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Reverse f a -> Reverse f b # catMaybes :: Reverse f (Maybe a) -> Reverse f a # filter :: (a -> Bool) -> Reverse f a -> Reverse f a # mapMaybeA :: (Traversable (Reverse f), Applicative p) => (a -> p (Maybe b)) -> Reverse f a -> p (Reverse f b) # filterA :: (Traversable (Reverse f), Applicative p) => (a -> p Bool) -> Reverse f a -> p (Reverse f a) # mapEither :: (a -> Either b c) -> Reverse f a -> (Reverse f b, Reverse f c) # mapEitherA :: (Traversable (Reverse f), Applicative p) => (a -> p (Either b c)) -> Reverse f a -> p (Reverse f b, Reverse f c) # partitionEithers :: Reverse f (Either a b) -> (Reverse f a, Reverse f b) #
(Filtrable f, Filtrable g) => Filtrable (Product f g) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Product f g a -> Product f g b # catMaybes :: Product f g (Maybe a) -> Product f g a # filter :: (a -> Bool) -> Product f g a -> Product f g a # mapMaybeA :: (Traversable (Product f g), Applicative p) => (a -> p (Maybe b)) -> Product f g a -> p (Product f g b) # filterA :: (Traversable (Product f g), Applicative p) => (a -> p Bool) -> Product f g a -> p (Product f g a) # mapEither :: (a -> Either b c) -> Product f g a -> (Product f g b, Product f g c) # mapEitherA :: (Traversable (Product f g), Applicative p) => (a -> p (Either b c)) -> Product f g a -> p (Product f g b, Product f g c) # partitionEithers :: Product f g (Either a b) -> (Product f g a, Product f g b) #
(Filtrable f, Filtrable g) => Filtrable (Sum f g) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Sum f g a -> Sum f g b # catMaybes :: Sum f g (Maybe a) -> Sum f g a # filter :: (a -> Bool) -> Sum f g a -> Sum f g a # mapMaybeA :: (Traversable (Sum f g), Applicative p) => (a -> p (Maybe b)) -> Sum f g a -> p (Sum f g b) # filterA :: (Traversable (Sum f g), Applicative p) => (a -> p Bool) -> Sum f g a -> p (Sum f g a) # mapEither :: (a -> Either b c) -> Sum f g a -> (Sum f g b, Sum f g c) # mapEitherA :: (Traversable (Sum f g), Applicative p) => (a -> p (Either b c)) -> Sum f g a -> p (Sum f g b, Sum f g c) # partitionEithers :: Sum f g (Either a b) -> (Sum f g a, Sum f g b) #
(Functor f, Filtrable g) => Filtrable (Compose f g) # |
Instance details Defined in Data.Filtrable Methods mapMaybe :: (a -> Maybe b) -> Compose f g a -> Compose f g b # catMaybes :: Compose f g (Maybe a) -> Compose f g a # filter :: (a -> Bool) -> Compose f g a -> Compose f g a # mapMaybeA :: (Traversable (Compose f g), Applicative p) => (a -> p (Maybe b)) -> Compose f g a -> p (Compose f g b) # filterA :: (Traversable (Compose f g), Applicative p) => (a -> p Bool) -> Compose f g a -> p (Compose f g a) # mapEither :: (a -> Either b c) -> Compose f g a -> (Compose f g b, Compose f g c) # mapEitherA :: (Traversable (Compose f g), Applicative p) => (a -> p (Either b c)) -> Compose f g a -> p (Compose f g b, Compose f g c) # partitionEithers :: Compose f g (Either a b) -> (Compose f g a, Compose f g b) #

(<$?>) :: Filtrable f => (a -> Maybe b) -> f a -> f b infixl 4 #

Infix synonym of `mapMaybe`

(<*?>) :: (Applicative p, Filtrable p) => p (a -> Maybe b) -> p a -> p b infixl 4 #

    f <*?> a = catMaybes (f <*> a)

nub :: (Filtrable f, Traversable f, Eq a) => f a -> f a #

\(\mathcal{O}(n^2)\) Delete all but the first copy of each element, special case of `nubBy`.

nubBy :: (Filtrable f, Traversable f) => (a -> a -> Bool) -> f a -> f a #

\(\mathcal{O}(n^2)\) Delete all but the first copy of each element, with the given relation.

nubOrd :: (Filtrable f, Traversable f, Ord a) => f a -> f a #

\(\mathcal{O}(n\;\mathrm{log}\;n)\) Delete all but the first copy of each element, special case of `nubOrdBy`.

nubOrdBy :: (Filtrable f, Traversable f) => (a -> a -> Ordering) -> f a -> f a #

\(\mathcal{O}(n\;\mathrm{log}\;n)\) Delete all but the first copy of each element, with the given relation.