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

Modulerebase-1.21.2Haskell2010

Rebase.Data.Traversable

  • 1 class
  • 8 values
  • Packagerebase-1.21.2
  • Exports9
  • LanguageHaskell2010
  • LicenceMIT
  • SourceTraversable.hs
classclass (Functor t, Foldable t) => Traversable (t :: Type -> Type) where
#

Functors representing data structures that can be transformed to structures of the same shape by performing an Applicative (or, therefore, Monad) action on each element from left to right.

A more detailed description of what same shape means, the various methods, how traversals are constructed, and example advanced use-cases can be found in the Overview section of Data.Traversable#overview.

For the class laws see the Laws section of Data.Traversable#laws.

Methods

  • traverse :: Applicative f => (a -> f b) -> t a -> f (t b)

    Map each element of a structure to an action, evaluate these actions from left to right, and collect the results. For a version that ignores the results see traverse_.

    Examples

    Basic usage:

    In the first two examples we show each evaluated action mapping to the output structure.

    Example1 expression
    traverse Just [1,2,3,4]Just [1,2,3,4]
    Example1 expression
    traverse id [Right 1, Right 2, Right 3, Right 4]Right [1,2,3,4]

    In the next examples, we show that Nothing and Left values short circuit the created structure.

    Example1 expression
    traverse (const Nothing) [1,2,3,4]Nothing
    Example1 expression
    traverse (\x -> if odd x then Just x else Nothing)  [1,2,3,4]Nothing
    Example1 expression
    traverse id [Right 1, Right 2, Right 3, Right 4, Left 0]Left 0
  • sequenceA :: Applicative f => t (f a) -> f (t a)

    Evaluate each action in the structure from left to right, and collect the results. For a version that ignores the results see sequenceA_.

    Examples

    Basic usage:

    For the first two examples we show sequenceA fully evaluating a a structure and collecting the results.

    Example1 expression
    sequenceA [Just 1, Just 2, Just 3]Just [1,2,3]
    Example1 expression
    sequenceA [Right 1, Right 2, Right 3]Right [1,2,3]

    The next two example show Nothing and Just will short circuit the resulting structure if present in the input. For more context, check the Traversable instances for Either and Maybe.

    Example1 expression
    sequenceA [Just 1, Just 2, Just 3, Nothing]Nothing
    Example1 expression
    sequenceA [Right 1, Right 2, Right 3, Left 4]Left 4
  • mapM :: Monad m => (a -> m b) -> t a -> m (t b)

    Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results. For a version that ignores the results see Data.Foldable.mapM_.

    Examples

    mapM is literally a traverse with a type signature restricted to Monad. Its implementation may be more efficient due to additional power of Monad.

  • sequence :: Monad m => t (m a) -> m (t a)

    Evaluate each monadic action in the structure from left to right, and collect the results. For a version that ignores the results see Data.Foldable.sequence_.

    Examples

    Basic usage:

    The first two examples are instances where the input and and output of sequence are isomorphic.

    Example1 expression
    sequence $ Right [1,2,3,4][Right 1,Right 2,Right 3,Right 4]
    Example1 expression
    sequence $ [Right 1,Right 2,Right 3,Right 4]Right [1,2,3,4]

    The following examples demonstrate short circuit behavior for sequence.

    Example1 expression
    sequence $ Left [1,2,3,4]Left [1,2,3,4]
    Example1 expression
    sequence $ [Left 0, Right 1,Right 2,Right 3,Right 4]Left 0
Instances106Traversable, …
valueforM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)
#

forM is mapM with its arguments flipped. For a version that ignores the results see Data.Foldable.forM_.

valuemapAccumL :: Traversable t => (s -> a -> (s, b)) -> s -> t a -> (s, t b)
#

The mapAccumL function behaves like a combination of fmap and foldl; it applies a function to each element of a structure, passing an accumulating parameter from left to right, and returning a final value of this accumulator together with the new structure.

Examples

Basic usage:

Example1 expression
mapAccumL (\a b -> (a + b, a)) 0 [1..10](55,[0,1,3,6,10,15,21,28,36,45])
Example1 expression
mapAccumL (\a b -> (a <> show b, a)) "0" [1..5]("012345",["0","01","012","0123","01234"])
valuemapAccumR :: Traversable t => (s -> a -> (s, b)) -> s -> t a -> (s, t b)
#

The mapAccumR function behaves like a combination of fmap and foldr; it applies a function to each element of a structure, passing an accumulating parameter from right to left, and returning a final value of this accumulator together with the new structure.

Examples

Basic usage:

Example1 expression
mapAccumR (\a b -> (a + b, a)) 0 [1..10](55,[54,52,49,45,40,34,27,19,10,0])
Example1 expression
mapAccumR (\a b -> (a <> show b, a)) "0" [1..5]("054321",["05432","0543","054","05","0"])
valuemapAccumM
  1. :: (Monad m, Traversable t)
  2. => s -> a -> m (s, b)
  3. -> s
  4. -> t a
  5. -> m (s, t b)
#

The mapAccumM function behaves like a combination of mapM and mapAccumL that traverses the structure while evaluating the actions and passing an accumulating parameter from left to right. It returns a final value of this accumulator together with the new structure. The accumulator is often used for caching the intermediate results of a computation.

Examples

Basic usage:

Example2 expressions
let expensiveDouble a = putStrLn ("Doubling " <> show a) >> pure (2 * a):{mapAccumM (\cache a -> case lookup a cache of    Nothing -> expensiveDouble a >>= \double -> pure ((a, double):cache, double)    Just double -> pure (cache, double)    ) [] [1, 2, 3, 1, 2, 3]:}Doubling 1Doubling 2Doubling 3([(3,6),(2,4),(1,2)],[2,4,6,2,4,6])