lens-3.7.4: 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. (Indexable 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

iwhereOf :: (Indexable i k, Applicative f) => Overloaded (Indexed 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 traversed (>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.

ignored :: forall k f i s a b. (Indexable i k, Applicative f) => Overloaded k f s s a bSource

This is the trivial empty traversal.

ignored :: IndexedTraversal i s s a b
ignoredconst pure

class At k m | m -> k whereSource

At provides a lens that can be used to read, write or delete the value associated with a key in a map-like container on an ad hoc basis.

Methods

at :: k -> SimpleIndexedLens k (m v) (Maybe v)Source

>>> Map.fromList [(1,"hello")] ^.at 1
Just "hello"
>>> at 1 ?~ "hello" $ Map.empty
fromList [(1,"hello")]

Note: Map-like containers form a reasonable instance, but not Array-like ones, where you cannot satisfy the Lens laws.

_at :: k -> SimpleIndexedTraversal k (m v) vSource

This simple indexed traversal lets you traverse the value at a given key in a map.

  • NB:* _setting_ the value of this lens will only set the value in the lens if it is already present.
_at k ≡ at k <. traverse

Instances

At Int IntMap 
(Eq k, Hashable k) => At k (HashMap k) 
Ord k => At k (Map k) 

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

traversed :: Traversable f => IndexedTraversal Int (f a) (f b) a bSource

Traverse any Traversable container. This is an IndexedTraversal that is indexed by ordinal position.

traversed64 :: Traversable f => IndexedTraversal Int64 (f a) (f b) a bSource

Traverse any Traversable container. This is an IndexedTraversal that is indexed by ordinal position.

elementOf :: (Applicative f, Indexable Int k) => LensLike (Indexing f) s t a a -> Int -> Overloaded k f s t a aSource

Traverse the nth element elementOf a Traversal, Lens or Iso if it exists.

>>> [[1],[3,4]] & elementOf (traverse.traverse) 1 .~ 5
[[1],[5,4]]
>>> [[1],[3,4]] ^? elementOf (folded.folded) 1
Just 3
>>> [0..] ^?! elementOf folded 5
5
>>> take 10 $ elementOf traverse 3 .~ 16 $ [0..]
[0,1,2,16,4,5,6,7,8,9]
 elementOf :: Simple Traversal s a -> Int -> SimpleIndexedTraversal Int s a
 elementOf :: Fold s a            -> Int -> IndexedFold Int s a

element :: Traversable t => Int -> SimpleIndexedTraversal Int (t a) aSource

Traverse the nth element of a Traversable container.

elementelementOf traverse

elementsOf :: (Applicative f, Indexable Int k) => LensLike (Indexing f) s t a a -> (Int -> Bool) -> Overloaded k f s t a aSource

Traverse (or fold) selected elements of a Traversal (or Fold) where their ordinal positions match a predicate.

 elementsOf :: Simple Traversal s a -> (Int -> Bool) -> SimpleIndexedTraversal Int s a
 elementsOf :: Fold s a            -> (Int -> Bool) -> IndexedFold Int s a

elements :: Traversable t => (Int -> Bool) -> SimpleIndexedTraversal Int (t a) aSource

Traverse elements of a Traversable container where their ordinal positions matches a predicate.

elementselementsOf traverse

Indexed Traversal Combinators

itraverseOf :: Overloaded (Indexed 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 (Indexed 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 (Indexed 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 (Indexed 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 (Indexed 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 (Indexed 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