Traversal

zio.blocks.schema.Traversal
See theTraversal companion object
sealed trait Traversal[S, A] extends Optic[S, A]

A traversal focuses on zero or more parts of S at once: every element of a collection, the elements at some indices, every key or value of a map, the values at some keys, or every value of a type found anywhere in S (searchFor). Composing any optic with a traversal gives a traversal.

fold and reduceOrFail read the parts, and modify updates all of them. When there are no parts, for example in an empty collection, fold returns its zero, modify returns a value equal to s, modifyOption returns None, and modifyOrFail and reduceOrFail return Left with an OpticCheck (OpticCheck.EmptySequence for an empty sequence).

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val tags: Traversal[Book, String] = $(_.tags.each)
}

val zionomicon = Book("Zionomicon", List("zio", "scala"))
Book.tags.fold[Int](zionomicon)(0, (count, _) => count + 1) // 2
Book.tags.modify(zionomicon, _.toUpperCase)                 // Book(Zionomicon,List(ZIO, SCALA))

Attributes

See also
Companion
object
Source
Optic.scala
Graph
Supertypes
trait Optic[S, A]
class Object
trait Matchable
class Any
Self type
Traversal[S, A]

Members list

Value members

Abstract methods

def fold[Z](s: S)(zero: Z, f: (Z, A) => Z): Z

Combines the parts of s that this traversal focuses on with f, starting from zero, in the order of the underlying collections. Returns zero when there are no parts. On Scala 2, give Z explicitly, as in fold[Int](s)(0, ...): Scala 2 cannot infer the parameter types of f from zero in the same parameter list.

Combines the parts of s that this traversal focuses on with f, starting from zero, in the order of the underlying collections. Returns zero when there are no parts. On Scala 2, give Z explicitly, as in fold[Int](s)(0, ...): Scala 2 cannot infer the parameter types of f from zero in the same parameter list.

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val tags: Traversal[Book, String] = $(_.tags.each)
}

Book.tags.fold[List[String]](Book("Zionomicon", List("zio", "scala")))(Nil, (acc, tag) => tag :: acc)
// List(scala, zio)

Attributes

Source
Optic.scala

Concrete methods

def apply[B](that: Lens[A, B]): Traversal[S, B]

Composes this traversal with that lens. The result is a Traversal.

Composes this traversal with that lens. The result is a Traversal.

Attributes

Source
Optic.scala
def apply[B <: A](that: Prism[A, B]): Traversal[S, B]

Composes this traversal with that prism. The result is a Traversal over the parts that are the case B.

Composes this traversal with that prism. The result is a Traversal over the parts that are the case B.

Attributes

Source
Optic.scala
def apply[B](that: Optional[A, B]): Traversal[S, B]

Composes this traversal with that optional. The result is a Traversal over the parts where that finds its focus.

Composes this traversal with that optional. The result is a Traversal over the parts where that finds its focus.

Attributes

Source
Optic.scala
def apply[B](that: Traversal[A, B]): Traversal[S, B]

Composes this traversal with that traversal. The result is a Traversal.

Composes this traversal with that traversal. The result is a Traversal.

Attributes

Source
Optic.scala
def reduceOrFail(s: S)(f: (A, A) => A): Either[OpticCheck, A]

Combines the parts of s with f, starting from the first part. Returns Left with the OpticCheck from check when there are no parts.

Combines the parts of s with f, starting from the first part. Returns Left with the OpticCheck from check when there are no parts.

Attributes

Source
Optic.scala

Inherited methods

final def !=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic differs from the literal that.

Builds a SchemaExpr that is true where the focus of this optic differs from the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def !=(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic differs from the focus of that.

Builds a SchemaExpr that is true where the focus of this optic differs from the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def %(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for the remainder of this optic's numeric focus divided by the literal that.

Builds a SchemaExpr for the remainder of this optic's numeric focus divided by the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def &(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for the bitwise and of this optic's integral focus and the literal that.

Builds a SchemaExpr for the bitwise and of this optic's integral focus and the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def &&(that: Boolean)(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

Builds a SchemaExpr for the logical and of this optic's Boolean focus and the literal that.

Builds a SchemaExpr for the logical and of this optic's Boolean focus and the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def &&(that: Optic[S, A])(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

Builds a SchemaExpr for the logical and of this optic's Boolean focus and the focus of that.

Builds a SchemaExpr for the logical and of this optic's Boolean focus and the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def *(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's numeric focus times the literal that.

Builds a SchemaExpr for this optic's numeric focus times the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def +(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's numeric focus plus the literal that.

Builds a SchemaExpr for this optic's numeric focus plus the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def -(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's numeric focus minus the literal that.

Builds a SchemaExpr for this optic's numeric focus minus the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def /(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's numeric focus divided by the literal that.

Builds a SchemaExpr for this optic's numeric focus divided by the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def <(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is less than the literal that.

Builds a SchemaExpr that is true where the focus of this optic is less than the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def <(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is less than the focus of that.

Builds a SchemaExpr that is true where the focus of this optic is less than the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def <<(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's integral focus shifted left by the literal that.

Builds a SchemaExpr for this optic's integral focus shifted left by the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def <=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the literal that.

Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def <=(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the focus of that.

Builds a SchemaExpr that is true where the focus of this optic is less than or equal to the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def ===(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic equals the focus of that.

Builds a SchemaExpr that is true where the focus of this optic equals the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def ===(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic equals the literal that. Evaluate it on an S with SchemaExpr#eval.

Builds a SchemaExpr that is true where the focus of this optic equals the literal that. Evaluate it on an S with SchemaExpr#eval.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is greater than the literal that.

Builds a SchemaExpr that is true where the focus of this optic is greater than the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is greater than the focus of that.

Builds a SchemaExpr that is true where the focus of this optic is greater than the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >=(that: A)(implicit schema: Schema[A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the literal that.

Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >=(that: Optic[S, A]): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the focus of that.

Builds a SchemaExpr that is true where the focus of this optic is greater than or equal to the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >>(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, keeping the sign.

Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, keeping the sign.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def >>>(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, filling with zeros.

Builds a SchemaExpr for this optic's integral focus shifted right by the literal that, filling with zeros.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def ^(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for the bitwise exclusive or of this optic's integral focus and the literal that.

Builds a SchemaExpr for the bitwise exclusive or of this optic's integral focus and the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def asEquivalent[B](implicit ev: A =:= B): Optic[S, B]

Returns this optic with its focus type changed to B, which ev proves is the same type. The optic is cast, not copied.

Returns this optic with its focus type changed to B, which ev proves is the same type. The optic is cast, not copied.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def concat(that: String)(implicit ev: A =:= String): SchemaExpr[S, String]

Builds a SchemaExpr for this optic's String focus followed by the literal that.

Builds a SchemaExpr for this optic's String focus followed by the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def length(implicit ev: A =:= String): SchemaExpr[S, Int]

Builds a SchemaExpr for the length of this optic's String focus.

Builds a SchemaExpr for the length of this optic's String focus.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def listValues[B](implicit ev: A =:= List[B]): Traversal[S, B]

Composes this optic, whose focus is a List, with a traversal over every element of that list.

Composes this optic, whose focus is a List, with a traversal over every element of that list.

Attributes

Throws
java.lang.RuntimeException

("Expected List") if the focus schema is not a List sequence schema

Inherited from:
Optic
Source
Optic.scala
final def matches(that: String)(implicit ev: A =:= String): SchemaExpr[S, Boolean]

Builds a SchemaExpr that is true where this optic's String focus matches the Java regular expression that as a whole (java.util.regex.Pattern.matches).

Builds a SchemaExpr that is true where this optic's String focus matches the Java regular expression that as a whole (java.util.regex.Pattern.matches).

Attributes

Inherited from:
Optic
Source
Optic.scala
final def pow(that: A)(implicit isNumeric: IsNumeric[A]): SchemaExpr[S, A]

Builds a SchemaExpr for this optic's numeric focus raised to the power of the literal that.

Builds a SchemaExpr for this optic's numeric focus raised to the power of the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def setValues[B](implicit ev: A =:= Set[B]): Traversal[S, B]

Composes this optic, whose focus is a Set, with a traversal over every element of that set.

Composes this optic, whose focus is a Set, with a traversal over every element of that set.

Attributes

Throws
java.lang.RuntimeException

("Expected Set") if the focus schema is not a Set sequence schema

Inherited from:
Optic
Source
Optic.scala
final def unary_!(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

Builds a SchemaExpr for the negation of this optic's Boolean focus.

Builds a SchemaExpr for the negation of this optic's Boolean focus.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def unary_~(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for the bitwise complement of this optic's integral focus.

Builds a SchemaExpr for the bitwise complement of this optic's integral focus.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def vectorValues[B](implicit ev: A =:= Vector[B]): Traversal[S, B]

Composes this optic, whose focus is a Vector, with a traversal over every element of that vector.

Composes this optic, whose focus is a Vector, with a traversal over every element of that vector.

Attributes

Throws
java.lang.RuntimeException

("Expected Vector") if the focus schema is not a Vector sequence schema

Inherited from:
Optic
Source
Optic.scala
final def |(that: A)(implicit ev: IsIntegral[A]): SchemaExpr[S, A]

Builds a SchemaExpr for the bitwise or of this optic's integral focus and the literal that.

Builds a SchemaExpr for the bitwise or of this optic's integral focus and the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def ||(that: Boolean)(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

Builds a SchemaExpr for the logical or of this optic's Boolean focus and the literal that.

Builds a SchemaExpr for the logical or of this optic's Boolean focus and the literal that.

Attributes

Inherited from:
Optic
Source
Optic.scala
final def ||(that: Optic[S, A])(implicit ev: A =:= Boolean): SchemaExpr[S, Boolean]

Builds a SchemaExpr for the logical or of this optic's Boolean focus and the focus of that.

Builds a SchemaExpr for the logical or of this optic's Boolean focus and the focus of that.

Attributes

Inherited from:
Optic
Source
Optic.scala

Inherited and Abstract methods

def check(s: S): Option[OpticCheck]

Returns None when this optic reaches its focus in s, and otherwise an OpticCheck that says why not. The possible problems are another case of a variant, a wrapper whose unwrap throws a SchemaError (both errors), an index past the end, a missing map key, an empty collection, and, for searchFor, no match at all (all four warnings). Except for searchFor, which decodes every candidate with its schema, check never runs a wrapper's wrap, so it does not see failures that only replace or modify meet.

Returns None when this optic reaches its focus in s, and otherwise an OpticCheck that says why not. The possible problems are another case of a variant, a wrapper whose unwrap throws a SchemaError (both errors), an index past the end, a missing map key, an empty collection, and, for searchFor, no match at all (all four warnings). Except for searchFor, which decodes every candidate with its schema, check never runs a wrapper's wrap, so it does not see failures that only replace or modify meet.

A Lens always returns None. A Prism and an Optional report the first problem on the path. A Traversal reports the problems in every branch it visits, even when it reaches other parts: atIndices(0, 2) on a one-element list reports index 2.

Attributes

Inherited from:
Optic
Source
Optic.scala
def focus: Bound[A]

The schema of the parts this optic focuses on. For a composed optic, it is the focus of the last step.

The schema of the parts this optic focuses on. For a composed optic, it is the focus of the last step.

Attributes

Inherited from:
Optic
Source
Optic.scala
def modify(s: S, f: A => A): S

Returns s with f applied to every part this optic focuses on. When there is nothing to focus on, the result equals s: a Prism on another case, an Optional on a missing index or key, a Traversal over an empty collection. It also equals s when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md); other exceptions, from the path or from f, propagate. modifyOption and modifyOrFail say when nothing was modified.

Returns s with f applied to every part this optic focuses on. When there is nothing to focus on, the result equals s: a Prism on another case, an Optional on a missing index or key, a Traversal over an empty collection. It also equals s when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md); other exceptions, from the path or from f, propagate. modifyOption and modifyOrFail say when nothing was modified.

case class Book(title: String, tags: List[String])
object Book extends CompanionOptics[Book] {
 implicit val schema: Schema[Book] = Schema.derived
 val tags: Traversal[Book, String] = $(_.tags.each)
}

Book.tags.modify(Book("Zionomicon", List("zio", "scala")), _.toUpperCase)
// Book(Zionomicon,List(ZIO, SCALA))

Attributes

Inherited from:
Optic
Source
Optic.scala
def modifyOption(s: S, f: A => A): Option[S]

Like modify, but returns None instead of an unchanged value when there is nothing to focus on, and when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Some.

Like modify, but returns None instead of an unchanged value when there is nothing to focus on, and when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Some.

Attributes

Inherited from:
Optic
Source
Optic.scala
def modifyOrFail(s: S, f: A => A): Either[OpticCheck, S]

Like modify, but returns Left with an OpticCheck that says why nothing was modified, as check does, and a OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Right. A Traversal returns Right once it has modified at least one part, even when other parts are missing; check reports those.

Like modify, but returns Left with an OpticCheck that says why nothing was modified, as check does, and a OpticCheck.WrappingError when a wrapped step on the path throws a SchemaError, including one that f throws below that step (a bug, see docs/dev/issues.md). A Lens always returns Right. A Traversal returns Right once it has modified at least one part, even when other parts are missing; check reports those.

Attributes

Inherited from:
Optic
Source
Optic.scala
def source: Bound[S]

The schema of the value this optic starts from. For a composed optic, it is the source of the first step.

The schema of the value this optic starts from. For a composed optic, it is the source of the first step.

Attributes

Inherited from:
Optic
Source
Optic.scala

Returns the path of this optic as a DynamicOptic, with one node per step. For example, $(_.format.when[Paperback].pages).toDynamic.toString is .format.pages.

Returns the path of this optic as a DynamicOptic, with one node per step. For example, $(_.format.when[Paperback].pages).toDynamic.toString is .format.pages.

Attributes

Inherited from:
Optic
Source
Optic.scala