witherable-0.2: filterable traversable

Copyright(c) Fumiaki Kinoshita 2015
LicenseBSD3
MaintainerFumiaki Kinoshita <fumiexcel@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Witherable

Contents

Description

 

Synopsis

Documentation

class Functor f => Filterable f where #

Like Functor, but it include Maybe effects.

Formally, the class Filterable represents a functor from Kleisli Maybe to Hask.

A definition of mapMaybe must satisfy the following laws:

identity
mapMaybe Just ≡ id
composition
mapMaybe f . mapMaybe g ≡ mapMaybe (f <=< g)

Minimal complete definition

mapMaybe | catMaybes

Methods

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

Like mapMaybe.

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

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

filter f . filter g ≡ filter (liftA2 (&&) f g)

Instances

Filterable [] # 

Methods

mapMaybe :: (a -> Maybe b) -> [a] -> [b] #

catMaybes :: [Maybe a] -> [a] #

filter :: (a -> Bool) -> [a] -> [a] #

Filterable Maybe # 

Methods

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

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

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

Filterable IntMap # 

Methods

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

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

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

Filterable Seq # 

Methods

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

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

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

Filterable Vector # 

Methods

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

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

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

Monoid e => Filterable (Either e) # 

Methods

mapMaybe :: (a -> Maybe b) -> Either e a -> Either e b #

catMaybes :: Either e (Maybe a) -> Either e a #

filter :: (a -> Bool) -> Either e a -> Either e a #

Filterable (Proxy *) # 

Methods

mapMaybe :: (a -> Maybe b) -> Proxy * a -> Proxy * b #

catMaybes :: Proxy * (Maybe a) -> Proxy * a #

filter :: (a -> Bool) -> Proxy * a -> Proxy * a #

Filterable (Map k) # 

Methods

mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b #

catMaybes :: Map k (Maybe a) -> Map k a #

filter :: (a -> Bool) -> Map k a -> Map k a #

Functor f => Filterable (MaybeT f) # 

Methods

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

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

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

(Eq k, Hashable k) => Filterable (HashMap k) # 

Methods

mapMaybe :: (a -> Maybe b) -> HashMap k a -> HashMap k b #

catMaybes :: HashMap k (Maybe a) -> HashMap k a #

filter :: (a -> Bool) -> HashMap k a -> HashMap k a #

Filterable (Const * r) # 

Methods

mapMaybe :: (a -> Maybe b) -> Const * r a -> Const * r b #

catMaybes :: Const * r (Maybe a) -> Const * r a #

filter :: (a -> Bool) -> Const * r a -> Const * r a #

(Functor f, Filterable g) => Filterable (Compose * * f g) # 

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 #

class (Traversable t, Filterable t) => Witherable t where #

Like Traversable, but you can remove elements instead of updating them.

A definition of wither must satisfy the following laws:

identity
wither (pure . Just) ≡ pure
composition
Compose . fmap (wither f) . wither g ≡ wither (Compose . fmap (wither f) . g)

Parametricity implies the naturality law:

t . wither f ≡ wither (t . f)

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> t a -> f (t b) #

traverse f ≡ wither (fmap Just . f)

filterA :: Applicative f => (a -> f Bool) -> t a -> f (t a) #

Compose . fmap (filterA f) . filterA g ≡ filterA (x -> Compose $ fmap (b -> (b&&) $ f x) (g x)

Instances

Witherable [] # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> [a] -> f [b] #

filterA :: Applicative f => (a -> f Bool) -> [a] -> f [a] #

Witherable Maybe # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Maybe a -> f (Maybe b) #

filterA :: Applicative f => (a -> f Bool) -> Maybe a -> f (Maybe a) #

Witherable IntMap # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> IntMap a -> f (IntMap b) #

filterA :: Applicative f => (a -> f Bool) -> IntMap a -> f (IntMap a) #

Witherable Seq # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Seq a -> f (Seq b) #

filterA :: Applicative f => (a -> f Bool) -> Seq a -> f (Seq a) #

Witherable Vector # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Vector a -> f (Vector b) #

filterA :: Applicative f => (a -> f Bool) -> Vector a -> f (Vector a) #

Monoid e => Witherable (Either e) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Either e a -> f (Either e b) #

filterA :: Applicative f => (a -> f Bool) -> Either e a -> f (Either e a) #

Witherable (Proxy *) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Proxy * a -> f (Proxy * b) #

filterA :: Applicative f => (a -> f Bool) -> Proxy * a -> f (Proxy * a) #

Witherable (Map k) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Map k a -> f (Map k b) #

filterA :: Applicative f => (a -> f Bool) -> Map k a -> f (Map k a) #

Traversable t => Witherable (MaybeT t) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> MaybeT t a -> f (MaybeT t b) #

filterA :: Applicative f => (a -> f Bool) -> MaybeT t a -> f (MaybeT t a) #

(Eq k, Hashable k) => Witherable (HashMap k) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> HashMap k a -> f (HashMap k b) #

filterA :: Applicative f => (a -> f Bool) -> HashMap k a -> f (HashMap k a) #

Witherable (Const * r) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Const * r a -> f (Const * r b) #

filterA :: Applicative f => (a -> f Bool) -> Const * r a -> f (Const * r a) #

(Traversable f, Witherable g) => Witherable (Compose * * f g) # 

Methods

wither :: Applicative f => (a -> f (Maybe b)) -> Compose * * f g a -> f (Compose * * f g b) #

filterA :: Applicative f => (a -> f Bool) -> Compose * * f g a -> f (Compose * * f g a) #

witherM :: (Witherable t, Monad m) => (a -> MaybeT m b) -> t a -> m (t b) #

A variant of wither that works on MaybeT.

blightM :: (Monad m, Witherable t) => t a -> (a -> MaybeT m b) -> m (t b) #

blightM is witherM with its arguments flipped.

ordNub :: (Witherable t, Ord a) => t a -> t a #

Removes duplicate elements from a list, keeping only the first occurrence. This is asymptotically faster than using nub from Data.List.

hashNub :: (Witherable t, Eq a, Hashable a) => t a -> t a #

Removes duplicate elements from a list, keeping only the first occurrence. This is usually faster than ordNub, especially for things that have a slow comparion (like String).

forMaybe :: (Witherable t, Applicative f) => t a -> (a -> f (Maybe b)) -> f (t b) #

Generalization

type FilterLike f s t a b = (a -> f (Maybe b)) -> s -> f t #

This type allows combinators to take a Filter specializing the parameter f.

type Filter s t a b = forall f. Applicative f => FilterLike f s t a b #

A Filter is like a Traversal, but you can also remove targets.

type FilterLike' f s a = FilterLike f s s a a #

A simple FilterLike.

type Filter' s a = forall f. Applicative f => FilterLike' f s a #

A simple Filter.

witherOf :: FilterLike f s t a b -> (a -> f (Maybe b)) -> s -> f t #

witherOf is actually id, but left for consistency.

forMaybeOf :: FilterLike f s t a b -> s -> (a -> f (Maybe b)) -> f t #

mapMaybeOf :: FilterLike Identity s t a b -> (a -> Maybe b) -> s -> t #

mapMaybe through a filter.

catMaybesOf :: FilterLike Identity s t (Maybe a) a -> s -> t #

catMaybes through a filter.

filterAOf :: Functor f => FilterLike' f s a -> (a -> f Bool) -> s -> f s #

filterA through a filter.

filterOf :: FilterLike' Identity s a -> (a -> Bool) -> s -> s #

Filter each element of a structure targeted by a Filter.

ordNubOf :: Ord a => FilterLike' (State (Set a)) s a -> s -> s #

Remove the duplicate elements through a filter.

hashNubOf :: (Eq a, Hashable a) => FilterLike' (State (HashSet a)) s a -> s -> s #

Remove the duplicate elements through a filter. It is often faster than ordNubOf, especially when the comparison is expensive.

Cloning

cloneFilter :: FilterLike (Peat a b) s t a b -> Filter s t a b #

Reconstitute a Filter from its monomorphic form.

newtype Peat a b t #

This is used to characterize and clone a Filter. Since FilterLike (Peat a b) s t a b is monomorphic, it can be used to store a filter in a container.

Constructors

Peat 

Fields

Instances

Functor (Peat a b) # 

Methods

fmap :: (a -> b) -> Peat a b a -> Peat a b b #

(<$) :: a -> Peat a b b -> Peat a b a #

Applicative (Peat a b) # 

Methods

pure :: a -> Peat a b a #

(<*>) :: Peat a b (a -> b) -> Peat a b a -> Peat a b b #

liftA2 :: (a -> b -> c) -> Peat a b a -> Peat a b b -> Peat a b c #

(*>) :: Peat a b a -> Peat a b b -> Peat a b b #

(<*) :: Peat a b a -> Peat a b b -> Peat a b a #