HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulewitherable-0.5Haskell2010

Witherable

  • 1 type
  • 4 classes
  • 7 values
  • Packagewitherable-0.5
  • Exports12
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceWitherable.hs
classclass Functor f => Filterable (f :: Type -> Type) where
#

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)

Methods

Instances27Filterable, …
value(<$?>) :: 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.

classclass (Traversable t, Filterable t) => Witherable (t :: Type -> Type) where
#

An enhancement of Traversable with Filterable

A definition of wither must satisfy the following laws:

identity

wither (Data.Functor.Identity . Just) ≡ Data.Functor.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 (Data.Functor.Identity . f) = Data.Functor.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

Methods

Instances27Witherable, …
valueordNub :: (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.

Example1 expression
ordNub [3,2,1,3,2,1][3,2,1]
valueordNubOn :: (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.

Example1 expression
ordNubOn fst [(True, 'x'), (False, 'y'), (True, 'z')][(True,'x'),(False,'y')]
valuehashNub :: (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).

Example1 expression
hashNub [3,2,1,3,2,1][3,2,1]
valuehashNubOn :: (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.

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

Indexed variants

2 declarations
classclass (FunctorWithIndex i t, Filterable t) => FilterableWithIndex i (t :: Type -> Type) | t -> i where
#

Indexed variant of Filterable.

Methods

Instances16FilterableWithIndex, …
classclass (TraversableWithIndex i t, FilterableWithIndex i t, Witherable t) => WitherableWithIndex i (t :: Type -> Type) | t -> i where
#

Indexed variant of Witherable.

Methods

Instances15WitherableWithIndex, …

Wrapper

1 declaration
newtypenewtype WrappedFoldable (f :: Type -> Type) a
#

Constructors

Instances11FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, FilterableWithIndex, Functor, Applicative, …