Traversal

dev.constructive.eo.optics.Traversal
See theTraversal companion object
abstract class Traversal[S, T, A, B] extends Optic[S, T, A, B, MultiFocus[PSVec]]

Concrete family class for Traversal — the many-focus optic on the MultiFocus[PSVec] carrier. Every constructor in the Traversal$ companion (and Each) returns this type, so "a Traversal[S, A]" is spelled Traversal[S, S, A, A] (like the four-parameter Optional and Modify classes). Ascribing it is SAFE — the fused members below are inline, so they survive ascription at THIS type (only ascribing the generic Optic[…] falls back to the generic extensions); capability evidence (CanFold[S, A], CanModify[S, A]) is served by the derived givens in each capability's companion.

'''Fused modify / replace / foldMap, by measurement''' (JMH TraversalBench.eoModify, size 64, -prof gc): the generic extensions summon the PARAMETERIZED mfFunctor[F: Functor] / mfFold[F: Foldable] givens — and a parameterized given instantiates per call — plus an extra capture in the spliced closure shape; together a fixed 40 B/op per operation (4 904 → 4 864 B/op measured, ±0.001). The members below splice the same logic with the CACHED PSVec.pSVecFunctor / pSVecFoldable instances instead. modify / replace are inline deliberately: each call site gets its own spliced body, so the to / from dispatch sites stay per-site monomorphic across the many subclasses (pEach / selfChildren / fixed-arity / the byte-carried integration traversals) — a plain def here would be ONE shared body accumulating every subclass's type profile, the megamorphic trap documented on Getter and PickFold (a plain-def variant was also measured: it recovers only 16 of the 40 B/op). foldMap is the deliberate exception — a virtual def the constructors override with STREAMING folds that skip to(s)'s focus-vector build entirely; see its scaladoc.

Each instance keeps its own existential X (the reassembly context: the original container for Traversal.pEach, the node for Traversal.selfChildren, Unit for the fixed-arity tabulations). The byte-carried integration traversals — eo-jsoniter's JsoniterTraversal and eo-avro's AvroTraversal — extend this class too (same Array[Byte] / MultiFocus[PSVec] shape), so ascribing them as Traversal[Array[Byte], Array[Byte], A, A] is safe; only eo-circe's JsonTraversal is NOT a subtype — its surface is a bespoke Ior[Chain[JsonFailure], _]-accumulating one, not the MultiFocus[PSVec] carrier.

Attributes

Companion
object
Source
Traversal.scala
Graph
Supertypes
trait Optic[S, T, A, B, MultiFocus[PSVec]]
class Object
trait Matchable
class Any
Known subtypes
class AvroTraversal[A]
class ComposedTraversal[S, T, A, B, C, D, Xo, Xi]
class TraverseTraversal[T, A, B]

Members list

Grouped members

Operations

Build-then-observe across the build-output ⇄ read-input seam, preserving structure, on a shared carrier F. Flip self (it must be reversible — Accessor[F] and ReverseAccessor[F], i.e. an Iso or Review over Direct) so it reads T from B, then andThen that under the same carrier. The result is the full Optic[B, A, C, D, F], not a collapsed getter: its read capability follows the carrier (.get for Direct), and self's read focus A survives as the composite's write-back focus.

Build-then-observe across the build-output ⇄ read-input seam, preserving structure, on a shared carrier F. Flip self (it must be reversible — Accessor[F] and ReverseAccessor[F], i.e. an Iso or Review over Direct) so it reads T from B, then andThen that under the same carrier. The result is the full Optic[B, A, C, D, F], not a collapsed getter: its read capability follows the carrier (.get for Direct), and self's read focus A survives as the composite's write-back focus.

This is exactly self.reverse.andThen(that). The motivating case is ana.cross(cata): a Review (the unfold) crossed with a getter on the built S (the fold) — a (materializing) hylomorphism whose .get reads the folded value. When that sits on a different carrier (a Prism, a Fold, …), the cross-carrier cross overload below is selected instead.

Seam: that's source is self's T and its back-type is self's S.

Attributes

Inherited from:
Optic
Source
Optic.scala

Type members

Inherited and Abstract types

type X

Existential leftover carried alongside the focus — the type-level witness the carrier uses to rebuild T. Concrete at construction (Lens.apply sets X = S, Prism.apply sets X = S, …) and abstract when the optic is bound to Optic[…, F] without refinement.

Existential leftover carried alongside the focus — the type-level witness the carrier uses to rebuild T. Concrete at construction (Lens.apply sets X = S, Prism.apply sets X = S, …) and abstract when the optic is bound to Optic[…, F] without refinement.

Attributes

Inherited from:
Optic
Source
Optic.scala

Value members

Concrete methods

def andThen[C, D](inner: Traversal[A, B, C, D]): Traversal[S, T, C, D]

Fused same-carrier Traversal.andThen(Traversal) — same composition as the generic Optic.andThen (the mfAssocPSVec kernel), re-homed under the concrete Traversal class via Traversal.composed. The generic member returns an anonymous Optic, so a composed chain's .modify / .foldMap resolve to the generic extensions — which re-instantiate the parameterized mfFunctor[PSVec] / mfFold[PSVec] givens per call (the measured 40 B/op of the class scaladoc) and share one megamorphic body. Returning Traversal keeps the fused inline members (and per-site monomorphic to / from dispatch) on every chain.

Fused same-carrier Traversal.andThen(Traversal) — same composition as the generic Optic.andThen (the mfAssocPSVec kernel), re-homed under the concrete Traversal class via Traversal.composed. The generic member returns an anonymous Optic, so a composed chain's .modify / .foldMap resolve to the generic extensions — which re-instantiate the parameterized mfFunctor[PSVec] / mfFold[PSVec] givens per call (the measured 40 B/op of the class scaladoc) and share one megamorphic body. Returning Traversal keeps the fused inline members (and per-site monomorphic to / from dispatch) on every chain.

Attributes

Source
Traversal.scala
def andThen[C, D](inner: GetReplaceLens[A, B, C, D]): Traversal[S, T, C, D]

Fused Traversal.andThen(Lens) — lifts the lens through the same tuple2multifocusPSVec bridge the Morph-routed generic extension uses, then composes on the shared carrier. See the andThen(inner: Traversal) overload for why the concrete return type matters.

Fused Traversal.andThen(Lens) — lifts the lens through the same tuple2multifocusPSVec bridge the Morph-routed generic extension uses, then composes on the shared carrier. See the andThen(inner: Traversal) overload for why the concrete return type matters.

Attributes

Source
Traversal.scala
def andThen[C, D, XI](inner: SplitCombineLens[A, B, C, D, XI]): Traversal[S, T, C, D]

Fused Traversal.andThen(Lens)SplitCombineLens / SimpleLens (macro-lens) inner.

Fused Traversal.andThen(Lens)SplitCombineLens / SimpleLens (macro-lens) inner.

Attributes

Source
Traversal.scala
def exists(p: A => Boolean)(s: S): Boolean

True iff a focus satisfies p, via the streaming foldMap under the disjunction monoid — member twin of the generic Optic.exists.

True iff a focus satisfies p, via the streaming foldMap under the disjunction monoid — member twin of the generic Optic.exists.

Attributes

Source
Traversal.scala
def foldMap[M](f: A => M)(s: S)(using M: Monoid[M]): M

Fused foldMap — the default folds the focus vector via the cached pSVecFoldable, skipping the per-call mfFold[PSVec] given instantiation the generic extension pays. Deliberately a plain overridable def, unlike modify: the constructors override it with STREAMING folds that never call to(s) at all — a fold has no use for the reassembly context to must pack, so materializing the focus vector is pure overhead on the read path. Traversal.pEach folds the container directly through Traverse[T], Traversal.selfChildren folds the children vector without the carrier wrapper, and Traversal.composed nests the two sides' folds so a depth-k chain streams with no arrays at any depth. The O(n) allocation win dwarfs the fixed per-call cost the old inline splice protected against.

Fused foldMap — the default folds the focus vector via the cached pSVecFoldable, skipping the per-call mfFold[PSVec] given instantiation the generic extension pays. Deliberately a plain overridable def, unlike modify: the constructors override it with STREAMING folds that never call to(s) at all — a fold has no use for the reassembly context to must pack, so materializing the focus vector is pure overhead on the read path. Traversal.pEach folds the container directly through Traverse[T], Traversal.selfChildren folds the children vector without the carrier wrapper, and Traversal.composed nests the two sides' folds so a depth-k chain streams with no arrays at any depth. The O(n) allocation win dwarfs the fixed per-call cost the old inline splice protected against.

Attributes

Source
Traversal.scala
def headOption(s: S): Option[A]

First focus, if any, via the streaming foldMap under a first-Some monoid — the member twin of the generic Optic.headOption extension, here so concrete Traversals skip the focus vector build (the extension routes through to(s)).

First focus, if any, via the streaming foldMap under a first-Some monoid — the member twin of the generic Optic.headOption extension, here so concrete Traversals skip the focus vector build (the extension routes through to(s)).

Attributes

Source
Traversal.scala
def length(s: S): Int

Focus count via the streaming foldMap — member twin of the generic Optic.length.

Focus count via the streaming foldMap — member twin of the generic Optic.length.

Attributes

Source
Traversal.scala
inline def modify(f: A => B): S => T

Fused modify — same logic as the generic extension, with the cached pSVecFunctor in place of the per-call mfFunctor[PSVec] given instantiation (−40 B/op with replace / foldMap's sibling savings; see the class scaladoc). inline so each call site splices its own copy — per-site monomorphic to / from dispatch.

Fused modify — same logic as the generic extension, with the cached pSVecFunctor in place of the per-call mfFunctor[PSVec] given instantiation (−40 B/op with replace / foldMap's sibling savings; see the class scaladoc). inline so each call site splices its own copy — per-site monomorphic to / from dispatch.

Attributes

Source
Traversal.scala
inline def replace(b: B): S => T

Fused replace — constant-function modify.

Fused replace — constant-function modify.

Attributes

Source
Traversal.scala

Inherited methods

def andThen[C, IB, G[_, _]](inner: Optic[A, Unit, C, IB, G])(using rc: ReadCompose[MultiFocus[PSVec], G]): rc.Out[S, C]

ANY outer ∘ read-only inner — the inner is honestly one-way (T = Unit: a Getter, AffineFold, or Fold), so only the two READ sides matter and the composite collapses to the read-only join of their strengths via compose.ReadCompose (Getter / PickFold / ForgetFold).

ANY outer ∘ read-only inner — the inner is honestly one-way (T = Unit: a Getter, AffineFold, or Fold), so only the two READ sides matter and the composite collapses to the read-only join of their strengths via compose.ReadCompose (Getter / PickFold / ForgetFold).

A trait member (not an extension in the companion) deliberately: once a receiver is statically one of the fused concrete classes, its andThen member overloads enter resolution and Scala 3 never falls back to extension methods when they all fail — the collapse must be in the member overload set to be reachable without an expected-type ascription.

Only the inner's T is pinned to Unit; its B stays free (IB) even though read-only inners always have B = Unit. That keeps this overload strictly LESS specific than the same-carrier andThen above (which accepts every argument this one does whenever B = Unit at the receiver), so a same-carrier read-only ∘ read-only call resolves unambiguously to the AssociativeFunctor path and this one fires exactly on the cross-seam cells the generic member cannot type.

Attributes

Inherited from:
Optic
Source
Optic.scala
inline def andThen[C, D](o: Optic[A, B, C, D, MultiFocus[PSVec]]): Optic[S, T, C, D, MultiFocus[PSVec]]

Compose with another optic under the shared carrier F. Requires AssociativeFunctor[F]. Cross-carrier composition (Lens → Optional, Lens → Traversal, …) goes through the Morph-summoning overload of this same method.

Compose with another optic under the shared carrier F. Requires AssociativeFunctor[F]. Cross-carrier composition (Lens → Optional, Lens → Traversal, …) goes through the Morph-summoning overload of this same method.

Attributes

Example
case class Address(street: String)
case class Person(address: Address)
val streetLens = lens[Person](_.address).andThen(lens[Address](_.street))
Inherited from:
Optic
Source
Optic.scala

Inherited and Abstract methods

def from(b: MultiFocus[PSVec][X, B]): T

Close the carrier: given a modified focus B (and the leftover X already inside the F), reassemble the result T.

Close the carrier: given a modified focus B (and the leftover X already inside the F), reassemble the result T.

Attributes

Inherited from:
Optic
Source
Optic.scala
def to(s: S): MultiFocus[PSVec][X, A]

Push the source S into the carrier, extracting the focus A and packing the leftover X. Paired with from to reconstruct T.

Push the source S into the carrier, extracting the focus A and packing the leftover X. Paired with from to reconstruct T.

Attributes

Inherited from:
Optic
Source
Optic.scala