lens-3.2: Lenses, Folds and Traversals

Portabilityrank 2 types, MPTCs, TFs, flexible
Stabilityprovisional
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellTrustworthy

Control.Lens.IndexedTraversal

Contents

Description

 

Synopsis

Indexed Traversals

type IndexedTraversal i s t a b = forall f k. (Indexed i k, Applicative f) => k (a -> f b) (s -> f t)Source

Every indexed traversal is a valid Traversal or IndexedFold.

The Indexed constraint is used to allow an IndexedTraversal to be used directly as a Traversal.

The Traversal laws are still required to hold.

Common Indexed Traversals

traverseAt :: At k m => k -> SimpleIndexedTraversal k (m v) vSource

Traverse the value at a given key in a map

traverseAt k = at k <. traverse

iwhereOf :: (Indexed i k, Applicative f) => Overloaded (Index i) f s t a a -> (i -> Bool) -> Overloaded k f s t a aSource

Access the element of an IndexedTraversal where the index matches a predicate.

>>> over (iwhereOf (indexed traverse) (>0)) reverse $ ["He","was","stressed","o_O"]
["He","saw","desserts","O_o"]
 iwhereOf :: IndexedFold i s a            -> (i -> Bool) -> IndexedFold i s a
 iwhereOf :: IndexedGetter i s a          -> (i -> Bool) -> IndexedFold i s a
 iwhereOf :: SimpleIndexedLens i s a      -> (i -> Bool) -> SimpleIndexedTraversal i s a
 iwhereOf :: SimpleIndexedTraversal i s a -> (i -> Bool) -> SimpleIndexedTraversal i s a
 iwhereOf :: SimpleIndexedSetter i s a    -> (i -> Bool) -> SimpleIndexedSetter i s a

value :: (k -> Bool) -> SimpleIndexedTraversal k (k, v) vSource

This provides a Traversal that checks a predicate on a key before allowing you to traverse into a value.

class Ord k => TraverseMin k m | m -> k whereSource

Allows IndexedTraversal the value at the smallest index.

Methods

traverseMin :: SimpleIndexedTraversal k (m v) vSource

IndexedTraversal of the element with the smallest index.

Instances

class Ord k => TraverseMax k m | m -> k whereSource

Allows IndexedTraversal of the value at the largest index.

Methods

traverseMax :: SimpleIndexedTraversal k (m v) vSource

IndexedTraversal of the element at the largest index.

Instances

Indexed Traversal Combinators

itraverseOf :: Overloaded (Index i) f s t a b -> (i -> a -> f b) -> s -> f tSource

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 :: IndexedLens i s t a b      -> (i -> a -> f b) -> s -> f t
 itraverseOf :: IndexedTraversal i s t a b -> (i -> a -> f b) -> s -> f t

iforOf :: Overloaded (Index i) f s t a b -> s -> (i -> a -> f b) -> f tSource

Traverse with an index (and the arguments flipped)

 forOf l a ≡ iforOf l a . const
 iforOfflip . itraverseOf
 iforOf :: IndexedLens i s t a b      -> s -> (i -> a -> f b) -> f t
 iforOf :: IndexedTraversal i s t a b -> s -> (i -> a -> f b) -> f t

imapMOf :: Overloaded (Index i) (WrappedMonad m) s t a b -> (i -> a -> m b) -> s -> m tSource

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, with access its position.

When you don't need access to the index mapMOf is more liberal in what it can accept.

mapMOf l ≡ imapMOf l . const
 imapMOf :: Monad m => IndexedLens      i s t a b -> (i -> a -> m b) -> s -> m t
 imapMOf :: Monad m => IndexedTraversal i s t a b -> (i -> a -> m b) -> s -> m t

iforMOf :: Overloaded (Index i) (WrappedMonad m) s t a b -> s -> (i -> a -> m b) -> m tSource

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, with access its position (and the arguments flipped).

 forMOf l a ≡ iforMOf l a . const
 iforMOfflip . imapMOf
 iforMOf :: Monad m => IndexedLens i s t a b      -> s -> (i -> a -> m b) -> m t
 iforMOf :: Monad m => IndexedTraversal i s t a b -> s -> (i -> a -> m b) -> m t

imapAccumROf :: Overloaded (Index i) (State s) s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)Source

Generalizes mapAccumR to an arbitrary IndexedTraversal with access to the index.

imapAccumROf accumulates state from right to left.

mapAccumROf l ≡ imapAccumROf l . const
 imapAccumROf :: IndexedLens i s t a b      -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)
 imapAccumROf :: IndexedTraversal i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)

imapAccumLOf :: Overloaded (Index i) (Backwards (State s)) s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)Source

Generalizes mapAccumL to an arbitrary IndexedTraversal with access to the index.

imapAccumLOf accumulates state from left to right.

mapAccumLOf l ≡ imapAccumLOf l . const
 imapAccumLOf :: IndexedLens i s t a b      -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)
 imapAccumLOf :: IndexedTraversal i s t a b -> (i -> s -> a -> (s, b)) -> s -> s -> (s, t)

Storing Indexed Traversals

newtype ReifiedIndexedTraversal i s t a b Source

Useful for storage.

Simple