Type synonym for a type-modifying indexed traversal.
Moduleoptics-core-0.4.1.1Haskell2010
Optics.IxTraversal
An IxTraversal is an indexed version of a Traversal.
See the "Indexed optics" section of the overview documentation in the
Optics module of the main optics package for more details on indexed
optics.
- 5 types
- 1 class
- 20 values
- Packageoptics-core-0.4.1.1
- Exports26
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceIxTraversal.hs
Formation
2 declarationsType synonym for a type-preserving indexed traversal.
Introduction
1 declarationBuild an indexed traversal from the van Laarhoven representation.
itraversalVL . itraverseOf ≡ id
itraverseOf . itraversalVL ≡ id
Elimination
1 declarationMap each element of a structure targeted by an IxTraversal (supplying the index), evaluate these actions from left to right, and collect the results.
This yields the van Laarhoven representation of an indexed traversal.
Computation
0 declarationsitraverseOf (itraversalVL f) ≡ f
Well-formedness
0 declarationsitraverseOf o (const pure) ≡ pure
fmap (itraverseOf o f) . itraverseOf o g ≡ getCompose . itraverseOf o (\ i -> Compose . fmap (f i) . g i)
Additional introduction forms
6 declarationsSee also each, which is an IxTraversal over each element of a (potentially monomorphic) container.
Indexed traversal via the TraversableWithIndex class.
itraverseOf itraversed ≡ itraverse
iover (itraversed <%> itraversed) (,) ["ab", "cd"][[((0,0),'a'),((0,1),'b')],[((1,0),'c'),((1,1),'d')]]
This is the trivial empty IxAffineTraversal, i.e. the optic that targets no substructures.
This is the identity element when a Fold, AffineFold, IxFold, IxAffineFold, Traversal or IxTraversal is viewed as a monoid.
6 & ignored %~ absurd6
Traverse selected elements of a Traversal where their ordinal positions match a predicate.
Traverse elements of a Traversable container where their ordinal positions match a predicate.
elements ≡ elementsOf traverse
Traverse the nth element of a Traversal if it exists.
Traverse the nth element of a Traversable container.
element ≡ elementOf traversed
Additional elimination forms
7 declarationsA version of itraverseOf with the arguments flipped.
Generalizes Data.Traversable.mapAccumL to an arbitrary IxTraversal.
imapAccumLOf accumulates state from left to right.
mapAccumLOf o ≡ imapAccumLOf o . const
Generalizes Data.Traversable.mapAccumR to an arbitrary IxTraversal.
imapAccumROf accumulates state from right to left.
mapAccumROf o ≡ imapAccumROf o . const
This permits the use of scanl1 over an arbitrary IxTraversal.
This permits the use of scanr1 over an arbitrary IxTraversal.
Try to map a function which uses the index over this IxTraversal, returning Nothing if the IxTraversal has no targets.
Version of ifailover strict in the application of the function.
Combinators
4 declarationsFilter results of an IxTraversal that don't satisfy a predicate on the indices.
toListOf (itraversed %& indices even) "foobar""foa"
This allows you to traverse the elements of an indexed traversal in the opposite order.
An indexed version of partsOf that receives the entire list of indices as its indices.
Convert an indexed traversal to an IxAffineTraversal that visits the first element of the original traversal.
For the fold version see ipre.
[1,2,3] & iover (isingular itraversed) (-)[-1,2,3]
Monoid structure
1 declarationIxTraversal admits a (partial) monoid structure where iadjoin combines non-overlapping indexed traversals, and the identity element is ignored (which traverses no elements).
If you merely need an IxFold, you can use indexed traversals as indexed
folds and combine them with one of the monoid structures on indexed folds
(see Optics.IxFold#monoids). In particular, isumming can be used to
concatenate results from two traversals, and ifailing will returns
results from the second traversal only if the first returns no results.
There is no Semigroup or Monoid instance for IxTraversal, because there is not a unique choice of monoid to use that works for all optics, and the (<>) operator could not be used to combine optics of different kinds.
Combine two disjoint indexed traversals into one.
iover (_1 % itraversed `iadjoin` _2 % itraversed) (+) ([0, 0, 0], (3, 5))([0,1,2],(3,8))
Note: if the argument traversals are not disjoint, the result will not respect the IxTraversal laws, because it will visit the same element multiple times. See section 7 of Understanding Idiomatic Traversals Backwards and Forwards by Bird et al. for why this is illegal.
iview (ipartsOf (each `iadjoin` each)) ("x","y")([0,1,0,1],["x","y","x","y"])iset (ipartsOf (each `iadjoin` each)) (const ["a","b","c","d"]) ("x","y")("c","d")
Subtyping
1 declarationTag for a traversal.
Instances28Is, JoinKinds, IxOptic, ToReadOnly, ReadOnlyOptic, …
Is A_Lens A_TraversalDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Prism A_TraversalDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Traversal A_FoldDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs A_Traversal A_SetterDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs An_AffineTraversal A_TraversalDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIs An_Iso A_TraversalDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Fold A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Getter A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_ReversedPrism A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_Fold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_Getter kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal A_ReversedPrism kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds A_Traversal An_AffineFold kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Fold => JoinKinds An_AffineFold A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds A_Setter A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Setter => JoinKinds A_Traversal A_Setter kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Lens A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Prism A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Lens kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Prism kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal An_AffineTraversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds A_Traversal An_Iso kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds An_AffineTraversal A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.Subtypingk ~ A_Traversal => JoinKinds An_Iso A_Traversal kDefined in optics-core-0.4.1.1 · Optics.Internal.Optic.SubtypingIxOptic A_Traversal s t a bDefined in optics-core-0.4.1.1 · Optics.Indexed.CoreToReadOnly A_Traversal s t a bDefined in optics-core-0.4.1.1 · Optics.ReadOnlytype ReadOnlyOptic A_Traversal = A_FoldDefined in optics-core-0.4.1.1 · Optics.ReadOnly
van Laarhoven encoding
2 declarationsThe van Laarhoven representation of an IxTraversal directly expresses
how it lifts an effectful operation I -> A -> F B on elements and their
indices to act on structures S -> F T. Thus itraverseOf converts an
IxTraversal to an IxTraversalVL.
type IxTraversalVL i s t a b = forall (f :: Type -> Type). Applicative f => (i -> a -> f b) -> s -> f tType synonym for a type-modifying van Laarhoven indexed traversal.
Type synonym for a type-preserving van Laarhoven indexed traversal.
Re-exports
1 declarationclass (FunctorWithIndex i t, FoldableWithIndex i t, Traversable t) => TraversableWithIndex i (t :: Type -> Type) | t -> i whereA Traversable with an additional index.
An instance must satisfy a (modified) form of the Traversable laws:
itraverse (const Identity) ≡ Identity
fmap (itraverse f) . itraverse g ≡ getCompose . itraverse (\i -> Compose . fmap (f i) . g i)
Methods
itraverse :: Applicative f => (i -> a -> f b) -> t a -> f (t b)Traverse an indexed container.
itraverse ≡itraverseOfitraversed
Instances28TraversableWithIndex, …
TraversableWithIndex Int IntMapDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Int SeqDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Int NonEmptyDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Int ZipListDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Int []Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex () IdentityDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex () Par1Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex () MaybeDefined in indexed-traversable-0.1.4 · WithIndexIx i => TraversableWithIndex i (Array i)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void ProxyDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void U1Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void V1Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex k (Map k)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex k (Tuple2 k)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void (Const e)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void (Constant e)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex i f => TraversableWithIndex i (Rec1 f)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex i f => TraversableWithIndex i (Backwards f)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex i f => TraversableWithIndex i (Reverse f)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex i m => TraversableWithIndex i (IdentityT m)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex Void (K1 i c)Defined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex [Int] TreeDefined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (Product f g)Defined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (Sum f g)Defined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (f :*: g)Defined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (Either i j) (f :+: g)Defined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (i, j) (Compose f g)Defined in indexed-traversable-0.1.4 · WithIndex(TraversableWithIndex i f, TraversableWithIndex j g) => TraversableWithIndex (i, j) (f :.: g)Defined in indexed-traversable-0.1.4 · WithIndex