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

Moduleindexed-traversable-0.1.4Haskell2010

Data.Traversable.WithIndex

Indexed Traversables

  • 1 class
  • 7 values

Indexed Traversables

1 declaration
classclass (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) => TraversableWithIndex i (t :: Type -> Type) | t -> i where
#

A Traversable with an additional index.

An instance must satisfy a (modified) form of the Traversable laws:

itraverse (const Identity) ≡ Identity
fmap (itraverse f) . itraverse g ≡ getCompose . itraverse (\i -> Compose . fmap (f i) . g i)

Methods

Instances28TraversableWithIndex, …

Indexed Traversable Combinators

5 declarations
valueimapM
  1. :: (TraversableWithIndex i t, Monad m)
  2. => i -> a -> m b
  3. -> t a
  4. -> m (t b)
#

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results, with access the index.

When you don't need access to the index mapM is more liberal in what it can accept.

mapM ≡ imapM . const
valueiforM
  1. :: (TraversableWithIndex i t, Monad m)
  2. => t a
  3. -> i -> a -> m b
  4. -> m (t b)
#

Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results, with access its position (and the arguments flipped).

forM a ≡ iforM a . const
iforM ≡ flip imapM
valueimapAccumR
  1. :: TraversableWithIndex i t
  2. => i -> s -> a -> (s, b)
  3. -> s
  4. -> t a
  5. -> (s, t b)
#

Generalizes Data.Traversable.mapAccumR to add access to the index.

imapAccumR accumulates state from right to left.

Data.Traversable.mapAccumR ≡ imapAccumR . const
valueimapAccumL
  1. :: TraversableWithIndex i t
  2. => i -> s -> a -> (s, b)
  3. -> s
  4. -> t a
  5. -> (s, t b)
#

Generalizes Data.Traversable.mapAccumL to add access to the index.

imapAccumL accumulates state from left to right.

Data.Traversable.mapAccumL ≡ imapAccumL . const

Default implementations

2 declarations