| Portability | Rank2Types |
|---|---|
| Stability | provisional |
| Maintainer | Edward Kmett <ekmett@gmail.com> |
| Safe Haskell | None |
Control.Lens.Traversal
Contents
Description
A is a generalization of Traversal a b c dtraverse from
Traversable. It allows you to traverse over a structure and change out
its contents with monadic or applicative side-effects. Starting from
,
traverse :: (Traversable t, Applicative f) => (c -> f d) -> t c -> f (t d)
we monomorphize the contents and result to obtain
type Traversal a b c d = forall f. Applicative f => (c -> f d) -> a -> f b
While a Traversal isn't quite a Fold, it _can_ be used for Getting
like a Fold, because given a Monoid m, we have an Applicative
for (. Everything you know how to do with a Const m)Traversable
container, you can with with a Traversal, and here we provide
combinators that generalize the usual Traversable operations.
- type Traversal a b c d = forall f. Applicative f => (c -> f d) -> a -> f b
- type :=> a b = forall f. Applicative f => (b -> f b) -> a -> f a
- element :: Traversable t => Int -> t a :-> a
- elementOf :: Functor f => LensLike (ElementOf f) a b c c -> Int -> LensLike f a b c c
- traverseOf :: LensLike f a b c d -> (c -> f d) -> a -> f b
- forOf :: LensLike f a b c d -> a -> (c -> f d) -> f b
- sequenceAOf :: LensLike f a b (f c) c -> a -> f b
- mapMOf :: LensLike (WrappedMonad m) a b c d -> (c -> m d) -> a -> m b
- forMOf :: LensLike (WrappedMonad m) a b c d -> a -> (c -> m d) -> m b
- sequenceOf :: LensLike (WrappedMonad m) a b (m c) c -> a -> m b
- transposeOf :: LensLike ZipList a b [c] c -> a -> [b]
- mapAccumLOf :: LensLike (Backwards (State s)) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
- mapAccumROf :: LensLike (State s) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
- scanr1Of :: LensLike (State (Maybe c)) a b c c -> (c -> c -> c) -> a -> b
- scanl1Of :: LensLike (Backwards (State (Maybe c))) a b c c -> (c -> c -> c) -> a -> b
- class (Functor t, Foldable t) => Traversable t where
- traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
- ignored :: Traversal a a c d
- traverseLeft :: Traversal (Either a c) (Either b c) a b
- traverseRight :: Traversal (Either c a) (Either c b) a b
- both :: Traversal (a, a) (b, b) a b
- cloneTraversal :: Applicative f => ((c -> Bazaar c d d) -> a -> Bazaar c d b) -> (c -> f d) -> a -> f b
- data ReifiedTraversal a b c d = ReifyTraversal {
- reflectTraversal :: Traversal a b c d
- type SimpleTraversal a b = Traversal a a b b
- type SimpleReifiedTraversal a b = ReifiedTraversal a a b b
Lenses
type Traversal a b c d = forall f. Applicative f => (c -> f d) -> a -> f bSource
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::Traversablef =>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".
tpure≡purefmap(t f).t g ≡getCompose.t (Compose.fmapf.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 :=> a b = forall f. Applicative f => (b -> f b) -> a -> f aSource
Lensing Traversals
element :: Traversable t => Int -> t a :-> aSource
Access the nth element of a Traversable container.
Attempts to access beyond the range of the Traversal will cause an error.
element≡elementOftraverse
Traversing and Lensing
traverseOf :: LensLike f a b c d -> (c -> f d) -> a -> f bSource
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≡id
This yields the obvious law:
traverse≡traverseOftraverse
traverseOf::Isoa b c d -> (c -> f d) -> a -> f btraverseOf::Lensa b c d -> (c -> f d) -> a -> f btraverseOf::Traversala b c d -> (c -> f d) -> a -> f b
forOf :: LensLike f a b c d -> a -> (c -> f d) -> f bSource
A version of traverseOf with the arguments flipped, such that:
forOfl ≡flip(traverseOfl)
for≡forOftraverse
This function is only provided for consistency, flip is strictly more general.
forOf≡flip
forOf::Isoa b c d -> a -> (c -> f d) -> f bforOf::Lensa b c d -> a -> (c -> f d) -> f bforOf::Traversala b c d -> a -> (c -> f d) -> f b
sequenceAOf :: LensLike f a b (f c) c -> a -> f bSource
Evaluate each action in the structure from left to right, and collect the results.
sequenceA≡sequenceAOftraverse≡traverseidsequenceAOfl ≡traverseOfl id ≡ l id
sequenceAOf::Isoa b (f c) c -> a -> f bsequenceAOf::Lensa b (f c) c -> a -> f bsequenceAOf::Applicativef =>Traversala b (f c) c -> a -> f b
mapMOf :: LensLike (WrappedMonad m) a b c d -> (c -> m d) -> a -> m bSource
Map each element of a structure targeted by a lens to a monadic action, evaluate these actions from left to right, and collect the results.
mapM≡mapMOftraverse
mapMOf::Isoa b c d -> (c -> m d) -> a -> m bmapMOf::Lensa b c d -> (c -> m d) -> a -> m bmapMOf::Monadm =>Traversala b c d -> (c -> m d) -> a -> m b
forMOf :: LensLike (WrappedMonad m) a b c d -> a -> (c -> m d) -> m bSource
sequenceOf :: LensLike (WrappedMonad m) a b (m c) c -> a -> m bSource
Sequence the (monadic) effects targeted by a lens in a container from left to right.
sequence≡sequenceOftraversesequenceOfl ≡mapMOfl idsequenceOfl ≡unwrapMonad. lWrapMonad
sequenceOf::Isoa b (m c) c -> a -> m bsequenceOf::Lensa b (m c) c -> a -> m bsequenceOf::Monadm =>Traversala b (m c) c -> a -> m b
transposeOf :: LensLike ZipList a b [c] c -> a -> [b]Source
This generalizes transpose to an arbitrary Traversal.
Note: transpose handles ragged inputs more intelligently, but for non-ragged inputs:
transpose≡transposeOftraverse
>>>transposeOf traverse [[1,2,3],[4,5,6]][[1,4],[2,5],[3,6]]
Since every Lens is a Traversal, we can use this as a form of
monadic strength as well:
transposeOf_2:: (b, [a]) -> [(b, a)]
mapAccumLOf :: LensLike (Backwards (State s)) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)Source
This generalizes mapAccumL to an arbitrary Traversal.
mapAccumL≡mapAccumLOftraverse
mapAccumLOf accumulates state from left to right.
mapAccumLOf::Isoa b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)mapAccumLOf::Lensa b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)mapAccumLOf::Traversala b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
mapAccumROf :: LensLike (State s) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)Source
This generalizes mapAccumR to an arbitrary Traversal.
mapAccumR≡mapAccumROftraverse
mapAccumROf accumulates state from right to left.
mapAccumROf::Isoa b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)mapAccumROf::Lensa b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)mapAccumROf::Traversala b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
Common Traversals
class (Functor t, Foldable t) => Traversable t where
Functors representing data structures that can be traversed from left to right.
Minimal complete definition: traverse or sequenceA.
Instances are similar to Functor, e.g. given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Traversable Tree where
traverse f Empty = pure Empty
traverse f (Leaf x) = Leaf <$> f x
traverse f (Node l k r) = Node <$> traverse f l <*> f k <*> traverse f r
This is suitable even for abstract types, as the laws for <*>
imply a form of associativity.
The superclass instances should satisfy the following:
- In the
Functorinstance,fmapshould be equivalent to traversal with the identity applicative functor (fmapDefault). - In the
Foldableinstance,foldMapshould be equivalent to traversal with a constant applicative functor (foldMapDefault).
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.
Instances
ignored :: Traversal a a c dSource
This is the trivial empty traversal.
ignored::Applicativef => (c -> f d) -> a -> f a
ignored≡constpure
traverseLeft :: Traversal (Either a c) (Either b c) a bSource
A traversal for tweaking the left-hand value of an Either:
>>>over traverseLeft (+1) (Left 2)Left 3>>>over traverseLeft (+1) (Right 2)Right 2>>>Right 42 ^.traverseLeft :: String"">>>Left "hello" ^.traverseLeft"hello"
traverseLeft ::Applicativef => (a -> f b) ->Eithera c -> f (Eitherb c)
traverseRight :: Traversal (Either c a) (Either c b) a bSource
traverse the right-hand value of an Either:
traverseRight≡traverse
Unfortunately the instance for
is still missing from base,
so this can't just be Traversable (Either c)traverse
>>>over traverseRight (+1) (Left 2)Left 2>>>over traverseRight (+1) (Right 2)Right 3>>>Right "hello" ^.traverseRight"hello">>>Left "hello" ^.traverseRight :: [Double][]
traverseRight ::Applicativef => (a -> f b) ->Eitherc a -> f (Eitherc a)
both :: Traversal (a, a) (b, b) a bSource
Traverse both parts of a tuple with matching types.
>>>both *~ 10 $ (1,2)(10,20)>>>over both length ("hello","world")(5,5)>>>("hello","world")^.both"helloworld"
Cloning Traversals
cloneTraversal :: Applicative f => ((c -> Bazaar c d d) -> a -> Bazaar c d b) -> (c -> f d) -> a -> f bSource
A Traversal is completely characterized by its behavior on a Bazaar.
Cloning a Traversal is one way to make sure you arent given
something weaker, such as a Fold and can be
used as a way to pass around traversals that have to be monomorphic in f.
Note: This only accepts a proper Traversal (or Lens). To clone a Lens
as such, use cloneLens
Note: It is usually better to ReifyTraversal and use reflectTraversal
than to cloneTraversal. The former can execute at full speed, while the
latter needs to round trip through the Bazaar.
>>>let foo l a = (view (cloneTraversal l) a, set (cloneTraversal l) 10 a)>>>foo both ("hello","world")("helloworld",(10,10))
cloneTraversal::LensLike(Bazaarc d) a b c d ->Traversala b c d
data ReifiedTraversal a b c d Source
A form of Traversal that can be stored monomorphically in a container.
Constructors
| ReifyTraversal | |
Fields
| |
Simple
type SimpleTraversal a b = Traversal a a b bSource
type SimpleReifiedTraversal a b = ReifiedTraversal a a b bSource
type SimpleReifiedTraversal =SimpleReifiedTraversal