planet-mitchell-0.0.0: Planet Mitchell

Safe HaskellSafe
LanguageHaskell2010

Optic.Traversal

Contents

Synopsis

Traversal

type Traversal s t a b = forall (f :: * -> *). Applicative f => (a -> f b) -> s -> f t #

A Traversal can be used directly as a Setter or a Fold (but not as a Lens) and provides the ability to both read and update multiple fields, subject to some relatively weak Traversal laws.

These have also been known as multilenses, but they have the signature and spirit of

traverse :: Traversable f => Traversal (f a) (f b) a b

and the more evocative name suggests their application.

Most of the time the Traversal you will want to use is just traverse, but you can also pass any Lens or Iso as a Traversal, and composition of a Traversal (or Lens or Iso) with a Traversal (or Lens or Iso) using (.) forms a valid Traversal.

The laws for a Traversal t follow from the laws for Traversable as stated in "The Essence of the Iterator Pattern".

t purepure
fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g)

One consequence of this requirement is that a Traversal needs to leave the same number of elements as a candidate for subsequent Traversal that it started with. Another testament to the strength of these laws is that the caveat expressed in section 5.5 of the "Essence of the Iterator Pattern" about exotic Traversable instances that traverse the same entry multiple times was actually already ruled out by the second law in that same paper!

type Traversal' s a = Traversal s s a a #

traverseOf :: LensLike f s t a b -> (a -> f b) -> s -> f t #

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

forOf :: LensLike f s t a b -> s -> (a -> f b) -> f t #

A version of traverseOf with the arguments flipped, such that:

>>> forOf each (1,2,3) print
1
2
3
((),(),())

This function is only provided for consistency, flip is strictly more general.

forOfflip
forOfflip . traverseOf
forforOf traverse
ifor l s ≡ for l s . Indexed
forOf :: Functor f => Iso s t a b -> s -> (a -> f b) -> f t
forOf :: Functor f => Lens s t a b -> s -> (a -> f b) -> f t
forOf :: Applicative f => Traversal s t a b -> s -> (a -> f b) -> f t

sequenceAOf :: LensLike f s t (f b) b -> s -> f t #

Evaluate each action in the structure from left to right, and collect the results.

>>> sequenceAOf both ([1,2],[3,4])
[(1,3),(1,4),(2,3),(2,4)]
sequenceAsequenceAOf traversetraverse id
sequenceAOf l ≡ traverseOf l id ≡ l id
sequenceAOf :: Functor f => Iso s t (f b) b       -> s -> f t
sequenceAOf :: Functor f => Lens s t (f b) b      -> s -> f t
sequenceAOf :: Applicative f => Traversal s t (f b) b -> s -> f t