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])
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.
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
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
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.
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]
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"])
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"])
This function may be used as a value for fmap in a Functor
instance, provided that traverse is defined. (Using
fmapDefault with a Traversable instance defined only by
sequenceA will result in infinite recursion.)
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])