traverse package:diagrams-lib

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.
>>> traverse Just [1,2,3,4]
Just [1,2,3,4]
>>> 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.
>>> traverse (const Nothing) [1,2,3,4]
Nothing
>>> traverse (\x -> if odd x then Just x else Nothing)  [1,2,3,4]
Nothing
>>> traverse id [Right 1, Right 2, Right 3, Right 4, Left 0]
Left 0
Traverse over all of the targets of a Fold1, computing an Apply based answer. As long as you have Applicative or Functor effect you are better using traverseOf_. The traverse1Of_ is useful only when you have genuine Apply effect.
>>> traverse1Of_ both1 (\ks -> Map.fromList [ (k, ()) | k <- ks ]) ("abc", "bcd")
fromList [('b',()),('c',())]
traverse1Of_ :: Apply f => Fold1 s a -> (a -> f r) -> s -> f ()
Traverse a container using its Traversable instance using explicitly provided Applicative operations. This is like traverse where the Applicative instance can be manually specified.
Traverse a container using a specified Applicative. This is like traverseBy where the Traversable instance can be specified by any Traversal
traverseByOf traversetraverseBy
IndexedTraversal of the element at the largest index.
IndexedTraversal of the element with the smallest index.
Map each element of a structure targeted by a Lens or Traversal, evaluate these actions from left to right, and collect the results. This function is only provided for consistency, id is strictly more general.
>>> traverseOf each print (1,2,3)
1
2
3
((),(),())
traverseOfid
itraverseOf l ≡ traverseOf l . Indexed
itraverseOf itraverseditraverse
This yields the obvious law:
traversetraverseOf traverse
traverseOf :: Functor f     => Iso s t a b        -> (a -> f b) -> s -> f t
traverseOf :: Functor f     => Lens s t a b       -> (a -> f b) -> s -> f t
traverseOf :: Apply f       => Traversal1 s t a b -> (a -> f b) -> s -> f t
traverseOf :: Applicative f => Traversal s t a b  -> (a -> f b) -> s -> f t
Traverse over all of the targets of a Fold (or Getter), computing an Applicative (or Functor)-based answer, but unlike traverseOf do not construct a new structure. traverseOf_ generalizes traverse_ to work over any Fold. When passed a Getter, traverseOf_ can work over any Functor, but when passed a Fold, traverseOf_ requires an Applicative.
>>> traverseOf_ both putStrLn ("hello","world")
hello
world
traverse_traverseOf_ folded
traverseOf_ _2 :: Functor f => (c -> f r) -> (d, c) -> f ()
traverseOf_ _Left :: Applicative f => (a -> f b) -> Either a c -> f ()
itraverseOf_ l ≡ traverseOf_ l . Indexed
The rather specific signature of traverseOf_ allows it to be used as if the signature was any of:
traverseOf_ :: Functor f     => Getter s a     -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Fold s a       -> (a -> f r) -> s -> f ()
traverseOf_ :: Functor f     => Lens' s a      -> (a -> f r) -> s -> f ()
traverseOf_ :: Functor f     => Iso' s a       -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Traversal' s a -> (a -> f r) -> s -> f ()
traverseOf_ :: Applicative f => Prism' s a     -> (a -> f r) -> s -> f ()
Traverse any Traversable container. This is an IndexedTraversal that is indexed by ordinal position.
Traverse any Traversable1 container. This is an IndexedTraversal1 that is indexed by ordinal position.
Traverse any Traversable container. This is an IndexedTraversal that is indexed by ordinal position.
Allows IndexedTraversal of the value at the largest index.
Allows IndexedTraversal the value at the smallest index.
Used internally by traverseOf_ and the like. The argument a of the result should not be used!
Traverse an indexed container.
itraverseitraverseOf itraversed
Traversal with an index. NB: When you don't need access to the index then you can just apply your IndexedTraversal directly as a function!
itraverseOfwithIndex
traverseOf l = itraverseOf l . const = id
itraverseOf :: Functor f     => IndexedLens i s t a b       -> (i -> a -> f b) -> s -> f t
itraverseOf :: Applicative f => IndexedTraversal i s t a b  -> (i -> a -> f b) -> s -> f t
itraverseOf :: Apply f       => IndexedTraversal1 i s t a b -> (i -> a -> f b) -> s -> f t
Traverse the targets of an IndexedFold or IndexedTraversal with access to the i, discarding the results. When you don't need access to the index then traverseOf_ is more flexible in what it accepts.
traverseOf_ l ≡ itraverseOf l . const
itraverseOf_ :: Functor f     => IndexedGetter i s a     -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Applicative f => IndexedFold i s a       -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Functor f     => IndexedLens' i s a      -> (i -> a -> f r) -> s -> f ()
itraverseOf_ :: Applicative f => IndexedTraversal' i s a -> (i -> a -> f r) -> s -> f ()
Traverse elements with access to the index i, discarding the results. When you don't need access to the index then traverse_ is more flexible in what it accepts.
traverse_ l = itraverse . const