Community

Witherable

provisional

class Functor f => Filterable f

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

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

A definition of mapMaybe must satisfy the following laws:

conservation

mapMaybe (Just . f) ≡ fmap f

composition

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

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

Like mapMaybe.

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

catMaybes ≡ mapMaybe id

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

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

drain :: f a -> f b

Empty a filterable.

drain ≡ mapMaybe (const Nothing)

(<$?>) :: Filterable f => (a -> Maybe b) -> f a -> f b

An infix alias for mapMaybe. The name of the operator alludes to <$>, and has the same fixity.

(<&?>) :: Filterable f => f a -> (a -> Maybe b) -> f b

Flipped version of <$?>, the Filterable version of <&>. It has the same fixity as <&>.

('<&?>') = 'flip' 'mapMaybe'

class (T.Traversable t, Filterable t) => Witherable t

An enhancement of Traversable with Filterable

A definition of wither must satisfy the following laws:

identity

wither (Identity . Just) ≡ Identity

composition

Compose . fmap (wither f) . wither g ≡ wither (Compose . fmap (wither f) . g)

Parametricity implies the naturality law:

naturality

t . wither f ≡ wither (t . f)

Where t is an /applicative transformation/ in the sense described in the Traversable documentation.

In the relation to superclasses, these should satisfy too:

conservation

wither (fmap Just . f) = traverse f

pure filter

wither (Identity . f) = Identity . mapMaybe f

See the Properties.md and Laws.md files in the git distribution for more in-depth explanation about properties of Witherable containers.

The laws and restrictions are enough to constrain wither to be uniquely determined as the following default implementation.

wither f = fmap catMaybes . traverse f

If not to provide better-performing implementation, it's not necessary to implement any one method of Witherable. For example, if a type constructor T already has instances of Traversable and Filterable, the next one line is sufficient to provide the Witherable T instance.

instance Witherable T

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

Effectful mapMaybe.

wither (pure . f) ≡ pure . mapMaybe f

witherM :: Monad m => (a -> m (Maybe b)) -> t a -> m (t b)

Monadic variant of wither. This may have more efficient implementation.

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

witherMap :: (Applicative m) => (t b -> r) -> (a -> m (Maybe b)) -> t a -> m r

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.

>>> ordNub [3,2,1,3,2,1]
[3,2,1]

ordNubOn :: (Witherable t, Ord b) => (a -> b) -> t a -> t a

The ordNubOn function behaves just like ordNub, except it uses a another type to determine equivalence classes.

>>> ordNubOn fst [(True, 'x'), (False, 'y'), (True, 'z')]
[(True,'x'),(False,'y')]

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 comparison (like String).

>>> hashNub [3,2,1,3,2,1]
[3,2,1]

hashNubOn :: (Witherable t, Eq b, Hashable b) => (a -> b) -> t a -> t a

The hashNubOn function behaves just like hashNub, except it uses a another type to determine equivalence classes.

>>> hashNubOn fst [(True, 'x'), (False, 'y'), (True, 'z')]
[(True,'x'),(False,'y')]

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

forMaybe = flip wither

Indexed variants

class (FunctorWithIndex i t, Filterable t) => FilterableWithIndex i t

Indexed variant of Filterable.

imapMaybe :: (i -> a -> Maybe b) -> t a -> t b

ifilter :: (i -> a -> Bool) -> t a -> t a

filter f . ifilter g ≡ ifilter (i x -> f x && g i x)

class (TraversableWithIndex i t, FilterableWithIndex i t, Witherable t) => WitherableWithIndex i t

Indexed variant of Witherable.

iwither :: (Applicative f) => (i -> a -> f (Maybe b)) -> t a -> f (t b)

Effectful imapMaybe.

iwither ( i -> pure . f i) ≡ pure . imapMaybe f

iwitherM :: (Monad m) => (i -> a -> m (Maybe b)) -> t a -> m (t b)

Monadic variant of wither. This may have more efficient implementation.

ifilterA :: (Applicative f) => (i -> a -> f Bool) -> t a -> f (t a)

Wrapper

Instances

94
  • instance Filterable Maybe
  • instance Witherable Maybe
  • instance Monoid e => Filterable (Either e)
  • instance Monoid e => Witherable (Either e)
  • instance Filterable []
  • instance Filterable ZipList
  • instance Witherable []

    Methods are good consumers for fusion.

  • instance Witherable ZipList
  • instance Filterable IM.IntMap
  • instance Witherable IM.IntMap
  • instance Filterable (M.Map k)
  • instance Witherable (M.Map k)
  • instance (Eq k, Hashable k) => Filterable (HM.HashMap k)
  • instance (Eq k, Hashable k) => Witherable (HM.HashMap k)
  • instance Filterable Proxy
  • instance Witherable Proxy
  • instance Filterable (Const r)
  • instance Witherable (Const r)
  • instance Filterable V.Vector
  • instance Witherable V.Vector
  • instance Filterable S.Seq
  • instance Witherable S.Seq
  • instance (Functor f, Filterable g) => Filterable (Compose f g)
  • instance (T.Traversable f, Witherable g) => Witherable (Compose f g)
  • instance (Filterable f, Filterable g) => Filterable (P.Product f g)
  • instance (Witherable f, Witherable g) => Witherable (P.Product f g)
  • instance (Filterable f, Filterable g) => Filterable (Sum.Sum f g)
  • instance (Witherable f, Witherable g) => Witherable (Sum.Sum f g)
  • instance Filterable f => Filterable (IdentityT f)
  • instance Witherable f => Witherable (IdentityT f)
  • instance Functor f => Filterable (MaybeT f)
  • instance (T.Traversable t) => Witherable (MaybeT t)
  • instance Filterable t => Filterable (Reverse t)
  • instance Witherable t => Witherable (Reverse t)

    Wither from right to left.

  • instance Filterable t => Filterable (Backwards t)
  • instance Witherable t => Witherable (Backwards t)
  • instance Filterable Generics.V1
  • instance Witherable Generics.V1
  • instance Filterable Generics.U1
  • instance Witherable Generics.U1
  • instance Filterable (Generics.K1 i c)
  • instance Witherable (Generics.K1 i c)
  • instance Filterable f => Filterable (Generics.Rec1 f)
  • instance Witherable f => Witherable (Generics.Rec1 f)
  • instance Filterable f => Filterable (Generics.M1 i c f)
  • instance Witherable f => Witherable (Generics.M1 i c f)
  • instance (Filterable f, Filterable g) => Filterable ((Generics.:*:) f g)
  • instance (Witherable f, Witherable g) => Witherable ((Generics.:*:) f g)
  • instance (Filterable f, Filterable g) => Filterable ((Generics.:+:) f g)
  • instance (Witherable f, Witherable g) => Witherable ((Generics.:+:) f g)
  • instance (Functor f, Filterable g) => Filterable ((Generics.:.:) f g)
  • instance (T.Traversable f, Witherable g) => Witherable ((Generics.:.:) f g)
  • instance FilterableWithIndex () Maybe
  • instance WitherableWithIndex () Maybe
  • instance FilterableWithIndex Int []
  • instance FilterableWithIndex Int ZipList
  • instance WitherableWithIndex Int []
  • instance WitherableWithIndex Int ZipList
  • instance FilterableWithIndex Int IM.IntMap
  • instance WitherableWithIndex Int IM.IntMap
  • instance FilterableWithIndex k (M.Map k)
  • instance WitherableWithIndex k (M.Map k)
  • instance (Eq k, Hashable k) => FilterableWithIndex k (HM.HashMap k)
  • instance (Eq k, Hashable k) => WitherableWithIndex k (HM.HashMap k)
  • instance FilterableWithIndex Void Proxy
  • instance WitherableWithIndex Void Proxy
  • instance FilterableWithIndex Int V.Vector
  • instance WitherableWithIndex Int V.Vector
  • instance FilterableWithIndex Int S.Seq
  • instance WitherableWithIndex Int S.Seq
  • instance (FunctorWithIndex i f, FilterableWithIndex j g) => FilterableWithIndex (i, j) (Compose f g)
  • instance (TraversableWithIndex i f, WitherableWithIndex j g) => WitherableWithIndex (i, j) (Compose f g)
  • instance (FilterableWithIndex i f, FilterableWithIndex j g) => FilterableWithIndex (Either i j) (P.Product f g)
  • instance (WitherableWithIndex i f, WitherableWithIndex j g) => WitherableWithIndex (Either i j) (P.Product f g)
  • instance (FilterableWithIndex i f, FilterableWithIndex j g) => FilterableWithIndex (Either i j) (Sum.Sum f g)
  • instance (WitherableWithIndex i f, WitherableWithIndex j g) => WitherableWithIndex (Either i j) (Sum.Sum f g)
  • instance (FilterableWithIndex i f) => FilterableWithIndex i (IdentityT f)
  • instance (WitherableWithIndex i f) => WitherableWithIndex i (IdentityT f)
  • instance FilterableWithIndex i t => FilterableWithIndex i (Reverse t)
  • instance WitherableWithIndex i t => WitherableWithIndex i (Reverse t)

    Wither from right to left.

  • instance FilterableWithIndex i t => FilterableWithIndex i (Backwards t)
  • instance WitherableWithIndex i t => WitherableWithIndex i (Backwards t)
  • instance Functor a => Functor (BoolPair a)
  • instance (Functor f, Functor a) => Functor (WrappedFoldable f a)
  • instance (Foldable f, Foldable a) => Foldable (WrappedFoldable f a)
  • instance (Traversable f, Traversable a) => Traversable (WrappedFoldable f a)
  • instance (Applicative f, Applicative a) => Applicative (WrappedFoldable f a)
  • instance (Alternative f, Alternative a) => Alternative (WrappedFoldable f a)
  • instance (FunctorWithIndex i f) => FunctorWithIndex i (WrappedFoldable f)
  • instance (FoldableWithIndex i f) => FoldableWithIndex i (WrappedFoldable f)
  • instance (TraversableWithIndex i f) => TraversableWithIndex i (WrappedFoldable f)
  • instance (F.Foldable f, Alternative f) => Filterable (WrappedFoldable f)
  • instance (FunctorWithIndex i f, FoldableWithIndex i f, Alternative f) => FilterableWithIndex i (WrappedFoldable f)
  • instance (Alternative f, T.Traversable f) => Witherable (WrappedFoldable f)
Maintainer
Fumiaki Kinoshita <fumiexcel@gmail.com>
Portability
non-portable
Copyright
(c) Fumiaki Kinoshita 2020
License
BSD3
Build record and source files

Community contribution. The service has not verified this documentation.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-858b1951-web-1

Metadata snapshot: 28fafa81682f94f7cb7646a350fe6773b76fc7fa2bd6bcfe9c6767c453c0c10e

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.