From 002990e9e7d3f3478c313fcef8c85393737a6313 Mon Sep 17 00:00:00 2001 From: Julien Truffaut Date: Mon, 8 Mar 2021 13:31:10 +0100 Subject: [PATCH] add inheritance for applied optics (#1116) --- .../main/scala/monocle/syntax/Applied.scala | 22 +-- .../scala/monocle/syntax/AppliedFold.scala | 133 ++++++++++------- .../scala/monocle/syntax/AppliedGetter.scala | 119 +++++++++------ .../scala/monocle/syntax/AppliedPIso.scala | 113 +++++++------- .../scala/monocle/syntax/AppliedPLens.scala | 133 ++++++++--------- .../monocle/syntax/AppliedPOptional.scala | 134 +++++++++-------- .../scala/monocle/syntax/AppliedPPrism.scala | 139 ++++++++---------- .../scala/monocle/syntax/AppliedPSetter.scala | 99 ++++++++----- .../monocle/syntax/AppliedPTraversal.scala | 121 ++++++++------- 9 files changed, 551 insertions(+), 462 deletions(-) diff --git a/core/shared/src/main/scala/monocle/syntax/Applied.scala b/core/shared/src/main/scala/monocle/syntax/Applied.scala index 72d4f2266..87476c5f3 100644 --- a/core/shared/src/main/scala/monocle/syntax/Applied.scala +++ b/core/shared/src/main/scala/monocle/syntax/Applied.scala @@ -9,33 +9,33 @@ object applied extends AppliedSyntax trait AppliedSyntax { implicit def toAppliedFoldOps[S](value: S): AppliedFoldOps[S] = - new AppliedFoldOps(value) + AppliedFoldOps(value) implicit def toAppliedGetterOps[S](value: S): AppliedGetterOps[S] = - new AppliedGetterOps(value) + AppliedGetterOps(value) implicit def toAppliedIsoOps[S](value: S): AppliedIsoOps[S] = - new AppliedIsoOps(value) + AppliedIsoOps(value) implicit def toAppliedLensOps[S](value: S): AppliedLensOps[S] = - new AppliedLensOps(value) + AppliedLensOps(value) implicit def toAppliedOptionalOps[S](value: S): AppliedOptionalOps[S] = - new AppliedOptionalOps(value) + AppliedOptionalOps(value) implicit def toAppliedPrismOps[S](value: S): AppliedPrismOps[S] = - new AppliedPrismOps(value) + AppliedPrismOps(value) implicit def toAppliedSetterOps[S](value: S): AppliedSetterOps[S] = - new AppliedSetterOps(value) + AppliedSetterOps(value) implicit def toAppliedTraversalOps[S](value: S): AppliedTraversalOps[S] = - new AppliedTraversalOps(value) + AppliedTraversalOps(value) } final case class AppliedFoldOps[S](private val s: S) extends AnyVal { @deprecated("use focus().andThen", since = "3.0.0-M1") def applyFold[A](fold: Fold[S, A]): AppliedFold[S, A] = - new AppliedFold[S, A](s, fold) + AppliedFold[S, A](s, fold) } final case class AppliedGetterOps[S](private val s: S) extends AnyVal { @deprecated("use focus().andThen", since = "3.0.0-M1") def applyGetter[A](getter: Getter[S, A]): AppliedGetter[S, A] = - new AppliedGetter[S, A](s, getter) + AppliedGetter[S, A](s, getter) } final case class AppliedIsoOps[S](private val s: S) extends AnyVal { @@ -85,7 +85,7 @@ final case class AppliedPrismOps[S](private val s: S) extends AnyVal { final case class AppliedSetterOps[S](private val s: S) extends AnyVal { @deprecated("use focus().andThen", since = "3.0.0-M1") def applySetter[T, A, B](setter: PSetter[S, T, A, B]): AppliedPSetter[S, T, A, B] = - new AppliedPSetter[S, T, A, B](s, setter) + AppliedPSetter[S, T, A, B](s, setter) } final case class AppliedTraversalOps[S](private val s: S) extends AnyVal { diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedFold.scala b/core/shared/src/main/scala/monocle/syntax/AppliedFold.scala index b7f9c5e23..4c399a685 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedFold.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedFold.scala @@ -2,95 +2,124 @@ package monocle.syntax import cats.Monoid import monocle.function.{At, Each, FilterIndex, Index} -import monocle.{std, Fold, Getter, Optional, PIso, PLens, POptional, PPrism, PTraversal} +import monocle._ -case class AppliedFold[S, A](s: S, _fold: Fold[S, A]) { - def foldMap[M: Monoid](f: A => M): M = _fold.foldMap(f)(s) +trait AppliedFold[S, A] { + def value: S + def optic: Fold[S, A] - def getAll: List[A] = _fold.getAll(s) - def find(p: A => Boolean): Option[A] = _fold.find(p)(s) - def headOption: Option[A] = _fold.headOption(s) - def lastOption: Option[A] = _fold.lastOption(s) - def exist(p: A => Boolean): Boolean = _fold.exist(p)(s) - def all(p: A => Boolean): Boolean = _fold.all(p)(s) - def length: Int = _fold.length(s) - def isEmpty: Boolean = _fold.isEmpty(s) - def nonEmpty: Boolean = _fold.nonEmpty(s) + def foldMap[M: Monoid](f: A => M): M = optic.foldMap(f)(value) + def getAll: List[A] = optic.getAll(value) + def find(p: A => Boolean): Option[A] = optic.find(p)(value) + def headOption: Option[A] = optic.headOption(value) + def lastOption: Option[A] = optic.lastOption(value) + def exist(p: A => Boolean): Boolean = optic.exist(p)(value) + def all(p: A => Boolean): Boolean = optic.all(p)(value) + def length: Int = optic.length(value) + def isEmpty: Boolean = optic.isEmpty(value) + def nonEmpty: Boolean = optic.nonEmpty(value) + + def some[A1](implicit ev1: A =:= Option[A1]): AppliedFold[S, A1] = + adapt[Option[A1]].andThen(std.option.some[A1]) + + private[monocle] def adapt[A1](implicit evA: A =:= A1): AppliedFold[S, A1] = + evA.substituteCo[AppliedFold[S, *]](this) + + def andThen[B](other: Fold[A, B]): AppliedFold[S, B] = + AppliedFold(value, optic.andThen(other)) +} + +object AppliedFold { + def apply[S, A](_value: S, _optic: Fold[S, A]): AppliedFold[S, A] = + new AppliedFold[S, A] { + val value: S = _value + val optic: Fold[S, A] = _optic + } + + implicit def appliedFoldSyntax[S, A](self: AppliedFold[S, A]): AppliedFoldSyntax[S, A] = + new AppliedFoldSyntax(self) +} + +final case class AppliedFoldSyntax[S, A](private val self: AppliedFold[S, A]) extends AnyVal { def each[C](implicit evEach: Each[A, C]): AppliedFold[S, C] = - andThen(evEach.each) + self.andThen(evEach.each) /** Select all the elements which satisfies the predicate. * This combinator can break the fusion property see Optional.filter for more details. */ def filter(predicate: A => Boolean): AppliedFold[S, A] = - andThen(Optional.filter(predicate)) + self.andThen(Optional.filter(predicate)) def filterIndex[I, A1](predicate: I => Boolean)(implicit ev: FilterIndex[A, I, A1]): AppliedFold[S, A1] = - andThen(ev.filterIndex(predicate)) + self.andThen(ev.filterIndex(predicate)) - def some[A1](implicit ev1: A =:= Option[A1]): AppliedFold[S, A1] = - adapt[Option[A1]].andThen(std.option.some[A1]) - - def withDefault[A1](defaultValue: A1)(implicit ev1: A =:= Option[A1]): AppliedFold[S, A1] = - adapt[Option[A1]].andThen(std.option.withDefault(defaultValue)) + def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedFold[S, A1] = + self.adapt[Option[A1]].andThen(std.option.withDefault(defaultValue)) def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedFold[S, A1] = - andThen(evAt.at(i)) + self.andThen(evAt.at(i)) def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedFold[S, A1] = - andThen(evIndex.index(i)) - - private def adapt[A1](implicit evA: A =:= A1): AppliedFold[S, A1] = - evA.substituteCo[AppliedFold[S, *]](this) - - def andThen[B](other: Fold[A, B]): AppliedFold[S, B] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B](other: Getter[A, B]): AppliedFold[S, B] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B, C, D](other: PLens[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, _fold.andThen(other)) - def andThen[B, C, D](other: PIso[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, _fold.andThen(other)) + self.andThen(evIndex.index(i)) + /** compose a [[Fold]] with a [[Fold]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[B](other: Fold[A, B]): AppliedFold[S, B] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[Getter]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeGetter[B](other: Getter[A, B]): AppliedFold[S, B] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PTraversal]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeTraversal[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[POptional]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeOptional[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PPrism]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composePrism[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PLens]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[B, C, D](other: PLens[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeLens[B, C, D](other: PLens[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PIso]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[B, C, D](other: PIso[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeIso[B, C, D](other: PIso[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^|->>[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^|-?[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^<-?[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[B, C, D](other: PLens[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^|->[B, C, D](other: PLens[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[B, C, D](other: PIso[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^<->[B, C, D](other: PIso[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) } diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedGetter.scala b/core/shared/src/main/scala/monocle/syntax/AppliedGetter.scala index abdabbee3..9b46bb461 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedGetter.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedGetter.scala @@ -1,75 +1,100 @@ package monocle.syntax -import monocle.function.{At, Each, FilterIndex, Index} -import monocle.{std, Fold, Getter, Optional, PIso, PLens, POptional, PPrism, PTraversal} +import monocle._ +import monocle.function.At -final case class AppliedGetter[S, A](s: S, getter: Getter[S, A]) { - def get: A = getter.get(s) - def exist(p: A => Boolean): Boolean = getter.exist(p)(s) - def find(p: A => Boolean): Option[A] = getter.find(p)(s) +trait AppliedGetter[S, A] extends AppliedFold[S, A] { - def each[C](implicit evEach: Each[A, C]): AppliedFold[S, C] = - andThen(evEach.each) + override def optic: Getter[S, A] - /** Select all the elements which satisfies the predicate. - * This combinator can break the fusion property see Optional.filter for more details. - */ - def filter(predicate: A => Boolean): AppliedFold[S, A] = - andThen(Optional.filter(predicate)) + def get: A = optic.get(value) - def filterIndex[I, A1](predicate: I => Boolean)(implicit ev: FilterIndex[A, I, A1]): AppliedFold[S, A1] = - andThen(ev.filterIndex(predicate)) - - def some[A1](implicit ev1: A =:= Option[A1]): AppliedFold[S, A1] = + override def some[A1](implicit ev1: A =:= Option[A1]): AppliedFold[S, A1] = adapt[Option[A1]].andThen(std.option.some[A1]) - def withDefault[A1](defaultValue: A1)(implicit ev1: A =:= Option[A1]): AppliedGetter[S, A1] = - adapt[Option[A1]].andThen(std.option.withDefault(defaultValue)) + override private[monocle] def adapt[A1](implicit evA: A =:= A1): AppliedGetter[S, A1] = + evA.substituteCo[AppliedGetter[S, *]](this) + + def andThen[B](other: Getter[A, B]): AppliedGetter[S, B] = + AppliedGetter(value, optic.andThen(other)) +} + +object AppliedGetter { + def apply[S, A](_value: S, _optic: Getter[S, A]): AppliedGetter[S, A] = + new AppliedGetter[S, A] { + val value: S = _value + val optic: Getter[S, A] = _optic + } + + implicit def appliedGetterSyntax[S, A](self: AppliedGetter[S, A]): AppliedGetterSyntax[S, A] = + new AppliedGetterSyntax(self) +} + +final case class AppliedGetterSyntax[S, A](private val self: AppliedGetter[S, A]) extends AnyVal { + + def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedGetter[S, A1] = + self.adapt[Option[A1]].andThen(std.option.withDefault(defaultValue)) def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedGetter[S, A1] = - andThen(evAt.at(i)) + self.andThen(evAt.at(i)) - def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedFold[S, A1] = - andThen(evIndex.index(i)) + /** compose a [[Fold]] with a [[Fold]] */ + @deprecated("use andThen", since = "3.0.0-M1") + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) - private def adapt[A1](implicit evA: A =:= A1): AppliedGetter[S, A1] = - evA.substituteCo[AppliedGetter[S, *]](this) + /** compose a [[Fold]] with a [[Getter]] */ + @deprecated("use andThen", since = "3.0.0-M1") + def composeGetter[C](other: Getter[A, C]): AppliedGetter[S, C] = + self.andThen(other) - def andThen[B](other: Fold[A, B]): AppliedFold[S, B] = - AppliedFold(s, getter.andThen(other)) - def andThen[B](other: Getter[A, B]): AppliedGetter[S, B] = - AppliedGetter(s, getter.andThen(other)) - def andThen[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, getter.andThen(other)) - def andThen[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, getter.andThen(other)) - def andThen[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = - AppliedFold(s, getter.andThen(other)) - def andThen[B, C, D](other: PLens[A, B, C, D]): AppliedGetter[S, C] = - AppliedGetter(s, getter.andThen(other)) - def andThen[B, C, D](other: PIso[A, B, C, D]): AppliedGetter[S, C] = - AppliedGetter(s, getter.andThen(other)) + /** compose a [[Fold]] with a [[PTraversal]] */ + @deprecated("use andThen", since = "3.0.0-M1") + def composeTraversal[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + /** compose a [[Fold]] with a [[POptional]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[B](other: Fold[A, B]): AppliedFold[S, B] = andThen(other) + def composeOptional[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PPrism]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeGetter[B](other: Getter[A, B]): AppliedGetter[S, B] = andThen(other) + def composePrism[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PLens]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeLens[B, C, D](other: PLens[A, B, C, D]): AppliedGetter[S, C] = + self.andThen(other) + + /** compose a [[Fold]] with a [[PIso]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def composeIso[B, C, D](other: PIso[A, B, C, D]): AppliedGetter[S, C] = + self.andThen(other) + + /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = andThen(other) + def ^|->>[B, C, D](other: PTraversal[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[B, C, D](other: PLens[A, B, C, D]): AppliedGetter[S, C] = andThen(other) + def ^|-?[B, C, D](other: POptional[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) + + /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[B, C, D](other: PIso[A, B, C, D]): AppliedGetter[S, C] = andThen(other) + def ^<-?[B, C, D](other: PPrism[A, B, C, D]): AppliedFold[S, C] = + self.andThen(other) /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[B, C, D](other: PLens[A, B, C, D]): AppliedGetter[S, C] = andThen(other) + def ^|->[B, C, D](other: PLens[A, B, C, D]): AppliedGetter[S, C] = + self.andThen(other) /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[B, C, D](other: PIso[A, B, C, D]): AppliedGetter[S, C] = andThen(other) + def ^<->[B, C, D](other: PIso[A, B, C, D]): AppliedGetter[S, C] = + self.andThen(other) } diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPIso.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPIso.scala index 87dae6649..07ca9971e 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPIso.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPIso.scala @@ -1,87 +1,96 @@ package monocle.syntax -import cats.Functor import monocle.function.{At, Each, FilterIndex, Index} import monocle._ -final case class AppliedPIso[S, T, A, B](s: S, iso: PIso[S, T, A, B]) { - def get: A = iso.get(s) - def replace(b: B): T = iso.replace(b)(s) - def modify(f: A => B): T = iso.modify(f)(s) - def modifyF[F[_]: Functor](f: A => F[B]): F[T] = iso.modifyF(f)(s) - def exist(p: A => Boolean): Boolean = iso.exist(p)(s) - def find(p: A => Boolean): Option[A] = iso.find(p)(s) +trait AppliedPIso[S, T, A, B] extends AppliedPLens[S, T, A, B] with AppliedPPrism[S, T, A, B] { - @deprecated("use replace instead", since = "3.0.0-M1") - def set(b: B): T = replace(b) + override def optic: PIso[S, T, A, B] - def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPPrism[S, T, A1, B1] = + override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPPrism[S, T, A1, B1] = adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) - private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPIso[S, T, A1, B1] = + override private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPIso[S, T, A1, B1] = evB.substituteCo[AppliedPIso[S, T, A1, *]](evA.substituteCo[AppliedPIso[S, T, *, B]](this)) - def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, iso.andThen(other)) - def andThen[C](other: Fold[A, C]): AppliedFold[S, C] = - AppliedFold(s, iso.andThen(other)) - def andThen[C](other: Getter[A, C]): AppliedGetter[S, C] = - AppliedGetter(s, iso.andThen(other)) - def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, iso.andThen(other)) - def andThen[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, iso.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = - AppliedPPrism(s, iso.andThen(other)) - def andThen[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = - AppliedPLens(s, iso.andThen(other)) def andThen[C, D](other: PIso[A, B, C, D]): AppliedPIso[S, T, C, D] = - AppliedPIso(s, iso.andThen(other)) + AppliedPIso(value, optic.andThen(other)) +} + +object AppliedPIso { + def apply[S, T, A, B](_value: S, _optic: PIso[S, T, A, B]): AppliedPIso[S, T, A, B] = + new AppliedPIso[S, T, A, B] { + val value: S = _value + val optic: PIso[S, T, A, B] = _optic + } + + implicit def appliedPIsoSyntax[S, T, A, B](self: AppliedPIso[S, T, A, B]): AppliedPIsoSyntax[S, T, A, B] = + new AppliedPIsoSyntax(self) + + implicit def appliedIsoSyntax[S, A](self: AppliedIso[S, A]): AppliedIsoSyntax[S, A] = + new AppliedIsoSyntax(self) +} + +object AppliedIso { + def apply[S, A](_value: S, _optic: Iso[S, A]): AppliedIso[S, A] = + AppliedIso(_value, _optic) +} + +final case class AppliedPIsoSyntax[S, T, A, B](private val self: AppliedPIso[S, T, A, B]) extends AnyVal { @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) - @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeGetter[C](other: Getter[A, C]): AppliedGetter[S, C] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedGetter[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPIso[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) - /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPIso[S, T, C, D] = + self.andThen(other) - /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) - /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPIso[S, T, C, D] = andThen(other) -} + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) -object AppliedPIso { - implicit def appliedIsoSyntax[S, A](self: AppliedIso[S, A]): AppliedIsoSyntax[S, A] = - new AppliedIsoSyntax(self) + @deprecated("use andThen", since = "3.0.0-M1") + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPIso[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplyIso */ final case class AppliedIsoSyntax[S, A](private val self: AppliedIso[S, A]) extends AnyVal { def each[C](implicit evEach: Each[A, C]): AppliedTraversal[S, C] = self.andThen(evEach.each) @@ -98,7 +107,7 @@ final case class AppliedIsoSyntax[S, A](private val self: AppliedIso[S, A]) exte def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedIso[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedLens[S, A1] = + def at[I, A1](i: I)(implicit evAt: At[A, I, A1]): AppliedLens[S, A1] = self.andThen(evAt.at(i)) def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedOptional[S, A1] = diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPLens.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPLens.scala index d07f2aca2..642f94d72 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPLens.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPLens.scala @@ -1,107 +1,104 @@ package monocle.syntax import cats.Functor -import monocle.function.{At, Each, FilterIndex, Index} import monocle._ +import monocle.function.At -final case class AppliedPLens[S, T, A, B](s: S, lens: PLens[S, T, A, B]) { - def get: A = lens.get(s) - def replace(b: B): T = lens.replace(b)(s) - def modify(f: A => B): T = lens.modify(f)(s) - def modifyF[F[_]: Functor](f: A => F[B]): F[T] = lens.modifyF(f)(s) - def exist(p: A => Boolean): Boolean = lens.exist(p)(s) - def find(p: A => Boolean): Option[A] = lens.find(p)(s) +trait AppliedPLens[S, T, A, B] extends AppliedPOptional[S, T, A, B] with AppliedGetter[S, A] { + override def optic: PLens[S, T, A, B] - /** alias to replace */ - @deprecated("use replace instead", since = "3.0.0-M1") - def set(b: B): T = replace(b) + def modifyF[F[_]: Functor](f: A => F[B]): F[T] = + optic.modifyF(f)(value) - def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPOptional[S, T, A1, B1] = + override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPOptional[S, T, A1, B1] = adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) - private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPLens[S, T, A1, B1] = + override private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPLens[S, T, A1, B1] = evB.substituteCo[AppliedPLens[S, T, A1, *]](evA.substituteCo[AppliedPLens[S, T, *, B]](this)) - def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, lens.andThen(other)) - def andThen[C](other: Fold[A, C]): AppliedFold[S, C] = - AppliedFold(s, lens.andThen(other)) - def andThen[C](other: Getter[A, C]): AppliedGetter[S, C] = - AppliedGetter(s, lens.andThen(other)) - def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, lens.andThen(other)) - def andThen[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, lens.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, lens.andThen(other)) def andThen[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = - AppliedPLens(s, lens.andThen(other)) - def andThen[C, D](other: PIso[A, B, C, D]): AppliedPLens[S, T, C, D] = - AppliedPLens(s, lens.andThen(other)) + AppliedPLens(value, optic.andThen(other)) + +} + +object AppliedPLens { + def apply[S, T, A, B](_value: S, _optic: PLens[S, T, A, B]): AppliedPLens[S, T, A, B] = + new AppliedPLens[S, T, A, B] { + val value: S = _value + val optic: PLens[S, T, A, B] = _optic + } + + implicit def appliedPLensSyntax[S, T, A, B](self: AppliedPLens[S, T, A, B]): AppliedPLensSyntax[S, T, A, B] = + new AppliedPLensSyntax(self) + + implicit def appliedLensSyntax[S, A](self: AppliedLens[S, A]): AppliedLensSyntax[S, A] = + new AppliedLensSyntax(self) +} + +object AppliedLens { + def apply[S, A](_value: S, _optic: Lens[S, A]): AppliedLens[S, A] = + AppliedPLens(_value, _optic) +} + +final case class AppliedPLensSyntax[S, T, A, B](private val self: AppliedPLens[S, T, A, B]) extends AnyVal { @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) - @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeGetter[C](other: Getter[A, C]): AppliedGetter[S, C] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedGetter[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) - /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) - /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPLens[S, T, C, D] = andThen(other) -} + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) -object AppliedPLens { - implicit def appliedLensSyntax[S, A](self: AppliedLens[S, A]): AppliedLensSyntax[S, A] = - new AppliedLensSyntax(self) + @deprecated("use andThen", since = "3.0.0-M1") + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPLens[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplyLens */ final case class AppliedLensSyntax[S, A](private val self: AppliedLens[S, A]) extends AnyVal { - def each[C](implicit evEach: Each[A, C]): AppliedTraversal[S, C] = - self.andThen(evEach.each) - - /** Select all the elements which satisfies the predicate. - * This combinator can break the fusion property see Optional.filter for more details. - */ - def filter(predicate: A => Boolean): AppliedOptional[S, A] = - self.andThen(Optional.filter(predicate)) - - def filterIndex[I, A1](predicate: I => Boolean)(implicit ev: FilterIndex[A, I, A1]): AppliedTraversal[S, A1] = - self.andThen(ev.filterIndex(predicate)) - def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedLens[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedLens[S, A1] = + def at[I, A1](i: I)(implicit evAt: At[A, I, A1]): AppliedLens[S, A1] = self.andThen(evAt.at(i)) - - def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedOptional[S, A1] = - self.andThen(evIndex.index(i)) } diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPOptional.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPOptional.scala index 0fbf52f42..b984e59c5 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPOptional.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPOptional.scala @@ -1,101 +1,108 @@ package monocle.syntax -import cats.Applicative -import monocle.function.{At, Each, FilterIndex, Index} import monocle._ +import monocle.function.{At, Index} -final case class AppliedPOptional[S, T, A, B](s: S, optional: POptional[S, T, A, B]) { - def getOption: Option[A] = optional.getOption(s) +trait AppliedPOptional[S, T, A, B] extends AppliedPTraversal[S, T, A, B] { + override def optic: POptional[S, T, A, B] - def isEmpty(s: S): Boolean = optional.isEmpty(s) - def nonEmpty(s: S): Boolean = optional.nonEmpty(s) - def all(p: A => Boolean): Boolean = optional.all(p)(s) - - def exist(p: A => Boolean): Boolean = optional.exist(p)(s) - def find(p: A => Boolean): Option[A] = optional.find(p)(s) - - def modify(f: A => B): T = optional.modify(f)(s) - def modifyA[F[_]: Applicative](f: A => F[B]): F[T] = - optional.modifyA(f)(s) - def modifyOption(f: A => B): Option[T] = optional.modifyOption(f)(s) - - def replace(b: B): T = optional.replace(b)(s) - def replaceOption(b: B): Option[T] = optional.replaceOption(b)(s) - - /** alias to replace */ - @deprecated("use replace instead", since = "3.0.0-M1") - def set(b: B): T = replace(b) + def getOption: Option[A] = optic.getOption(value) + def modifyOption(f: A => B): Option[T] = optic.modifyOption(f)(value) + def replaceOption(b: B): Option[T] = optic.replaceOption(b)(value) /** alias to replace */ @deprecated("use replaceOption instead", since = "3.0.0-M1") def setOption(b: B): Option[T] = replaceOption(b) - def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPOptional[S, T, A1, B1] = + override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPOptional[S, T, A1, B1] = adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) - private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPOptional[S, T, A1, B1] = + override private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPOptional[S, T, A1, B1] = evB.substituteCo[AppliedPOptional[S, T, A1, *]](evA.substituteCo[AppliedPOptional[S, T, *, B]](this)) - def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, optional.andThen(other)) - def andThen[C](other: Fold[A, C]): AppliedFold[S, C] = - AppliedFold(s, optional.andThen(other)) - def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, optional.andThen(other)) def andThen[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, optional.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, optional.andThen(other)) - def andThen[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, optional.andThen(other)) - def andThen[C, D](other: PIso[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, optional.andThen(other)) + AppliedPOptional(value, optic.andThen(other)) +} + +object AppliedPOptional { + def apply[S, T, A, B](_value: S, _optic: POptional[S, T, A, B]): AppliedPOptional[S, T, A, B] = + new AppliedPOptional[S, T, A, B] { + val value: S = _value + val optic: POptional[S, T, A, B] = _optic + } + + implicit def appliedPOptionalSyntax[S, T, A, B]( + self: AppliedPOptional[S, T, A, B] + ): AppliedPOptionalSyntax[S, T, A, B] = + new AppliedPOptionalSyntax(self) + + implicit def appliedOptionalSyntax[S, A](self: AppliedOptional[S, A]): AppliedOptionalSyntax[S, A] = + new AppliedOptionalSyntax(self) +} + +object AppliedOptional { + def apply[S, A](_value: S, _optic: Optional[S, A]): AppliedOptional[S, A] = + AppliedPOptional(_value, _optic) +} + +final case class AppliedPOptionalSyntax[S, T, A, B](private val self: AppliedPOptional[S, T, A, B]) extends AnyVal { @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) -} + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) -object AppliedPOptional { - implicit def appliedOptionalSyntax[S, A](self: AppliedOptional[S, A]): AppliedOptionalSyntax[S, A] = - new AppliedOptionalSyntax(self) + @deprecated("use andThen", since = "3.0.0-M1") + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplyOptional */ +/** Extension methods for monomorphic Optional + */ final case class AppliedOptionalSyntax[S, A](private val self: AppliedOptional[S, A]) extends AnyVal { - def each[C](implicit evEach: Each[A, C]): AppliedPTraversal[S, S, C, C] = - self.andThen(evEach.each) /** Select all the elements which satisfies the predicate. * This combinator can break the fusion property see Optional.filter for more details. @@ -103,13 +110,10 @@ final case class AppliedOptionalSyntax[S, A](private val self: AppliedOptional[S def filter(predicate: A => Boolean): AppliedOptional[S, A] = self.andThen(Optional.filter(predicate)) - def filterIndex[I, A1](predicate: I => Boolean)(implicit ev: FilterIndex[A, I, A1]): AppliedTraversal[S, A1] = - self.andThen(ev.filterIndex(predicate)) - def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedOptional[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedOptional[S, A1] = + def at[I, A1](i: I)(implicit evAt: At[A, I, A1]): AppliedOptional[S, A1] = self.andThen(evAt.at(i)) def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedOptional[S, A1] = diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPPrism.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPPrism.scala index 81ede0ea5..0e24cdcd3 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPPrism.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPPrism.scala @@ -1,115 +1,96 @@ package monocle.syntax -import cats.Applicative -import monocle.function.{At, Each, FilterIndex, Index} import monocle._ -final case class AppliedPPrism[S, T, A, B](s: S, prism: PPrism[S, T, A, B]) { - def getOption: Option[A] = prism.getOption(s) +trait AppliedPPrism[S, T, A, B] extends AppliedPOptional[S, T, A, B] { + override def optic: PPrism[S, T, A, B] - def modify(f: A => B): T = prism.modify(f)(s) - def modifyA[F[_]: Applicative](f: A => F[B]): F[T] = - prism.modifyA(f)(s) - def modifyOption(f: A => B): Option[T] = prism.modifyOption(f)(s) + override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPPrism[S, T, A1, B1] = + adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) - def replace(b: B): T = prism.replace(b)(s) - def replaceOption(b: B): Option[T] = prism.replaceOption(b)(s) - def isEmpty: Boolean = prism.isEmpty(s) - def nonEmpty: Boolean = prism.nonEmpty(s) - def find(p: A => Boolean): Option[A] = prism.find(p)(s) - def exist(p: A => Boolean): Boolean = prism.exist(p)(s) - def all(p: A => Boolean): Boolean = prism.all(p)(s) + override private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPPrism[S, T, A1, B1] = + evB.substituteCo[AppliedPPrism[S, T, A1, *]](evA.substituteCo[AppliedPPrism[S, T, *, B]](this)) - /** alias to replace */ - @deprecated("use replace instead", since = "3.0.0-M1") - def set(b: B): T = replace(b) + def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = + AppliedPPrism(value, optic.andThen(other)) - /** alias to replaceOption */ - @deprecated("use replaceOption instead", since = "3.0.0-M1") - def setOption(b: B): Option[T] = replaceOption(b) +} - def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPPrism[S, T, A1, B1] = - adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) +object AppliedPPrism { + def apply[S, T, A, B](_value: S, _optic: PPrism[S, T, A, B]): AppliedPPrism[S, T, A, B] = + new AppliedPPrism[S, T, A, B] { + val value: S = _value + val optic: PPrism[S, T, A, B] = _optic + } - private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPPrism[S, T, A1, B1] = - evB.substituteCo[AppliedPPrism[S, T, A1, *]](evA.substituteCo[AppliedPPrism[S, T, *, B]](this)) + implicit def appliedPPrismSyntax[S, T, A, B](self: AppliedPPrism[S, T, A, B]): AppliedPPrismSyntax[S, T, A, B] = + new AppliedPPrismSyntax(self) - def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, prism.andThen(other)) - def andThen[C](other: Fold[A, C]): AppliedFold[S, C] = - AppliedFold(s, prism.andThen(other)) - def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, prism.andThen(other)) - def andThen[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, prism.andThen(other)) - def andThen[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = - AppliedPOptional(s, prism.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = - AppliedPPrism(s, prism.andThen(other)) - def andThen[C, D](other: PIso[A, B, C, D]): AppliedPPrism[S, T, C, D] = - AppliedPPrism(s, prism.andThen(other)) + implicit def appliedPrismSyntax[S, A](self: AppliedPrism[S, A]): AppliedPrismSyntax[S, A] = + new AppliedPrismSyntax(self) +} + +object AppliedPrism { + def apply[S, A](_value: S, _optic: Prism[S, A]): AppliedPrism[S, A] = + AppliedPPrism(_value, _optic) +} + +final case class AppliedPPrismSyntax[S, T, A, B](private val self: AppliedPPrism[S, T, A, B]) extends AnyVal { @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) - /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) - /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) - /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) - /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPPrism[S, T, C, D] = andThen(other) -} + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPOptional[S, T, C, D] = + self.andThen(other) -object AppliedPPrism { - implicit def appliedPrismSyntax[S, A](self: AppliedPrism[S, A]): AppliedPrismSyntax[S, A] = - new AppliedPrismSyntax(self) + @deprecated("use andThen", since = "3.0.0-M1") + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPPrism[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplyPrism */ final case class AppliedPrismSyntax[S, A](private val self: AppliedPrism[S, A]) extends AnyVal { - def each[C](implicit evEach: Each[A, C]): AppliedTraversal[S, C] = - self.andThen(evEach.each) - - /** Select all the elements which satisfies the predicate. - * This combinator can break the fusion property see Optional.filter for more details. - */ - def filter(predicate: A => Boolean): AppliedOptional[S, A] = - self.andThen(Optional.filter(predicate)) - - def filterIndex[I, A1](predicate: I => Boolean)(implicit ev: FilterIndex[A, I, A1]): AppliedTraversal[S, A1] = - self.andThen(ev.filterIndex(predicate)) - def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedPrism[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedOptional[S, A1] = - self.andThen(evAt.at(i)) - - def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedOptional[S, A1] = - self.andThen(evIndex.index(i)) } diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPSetter.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPSetter.scala index 982ebf0e2..7a078d40b 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPSetter.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPSetter.scala @@ -3,9 +3,12 @@ package monocle.syntax import monocle.function.{At, Each, FilterIndex, Index} import monocle._ -final case class AppliedPSetter[S, T, A, B](s: S, setter: PSetter[S, T, A, B]) { - def replace(b: B): T = setter.replace(b)(s) - def modify(f: A => B): T = setter.modify(f)(s) +trait AppliedPSetter[S, T, A, B] { + def value: S + def optic: PSetter[S, T, A, B] + + def replace(b: B): T = optic.replace(b)(value) + def modify(f: A => B): T = optic.modify(f)(value) /** alias to replace */ @deprecated("use replace instead", since = "3.0.0-M1") @@ -18,60 +21,90 @@ final case class AppliedPSetter[S, T, A, B](s: S, setter: PSetter[S, T, A, B]) { evB.substituteCo[AppliedPSetter[S, T, A1, *]](evA.substituteCo[AppliedPSetter[S, T, *, B]](this)) def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) - def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) - def andThen[C, D](other: POptional[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) - def andThen[C, D](other: PLens[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) - def andThen[C, D](other: PIso[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, setter.andThen(other)) + AppliedPSetter(value, optic.andThen(other)) +} + +object AppliedPSetter { + def apply[S, T, A, B](_value: S, _optic: PSetter[S, T, A, B]): AppliedPSetter[S, T, A, B] = + new AppliedPSetter[S, T, A, B] { + val value: S = _value + val optic: PSetter[S, T, A, B] = _optic + } + + implicit def appliedPSetterSyntax[S, T, A, B](self: AppliedPSetter[S, T, A, B]): AppliedPSetterSyntax[S, T, A, B] = + new AppliedPSetterSyntax(self) + + implicit def appliedSetterSyntax[S, A](self: AppliedSetter[S, A]): AppliedSetterSyntax[S, A] = + new AppliedSetterSyntax(self) +} + +object AppliedSetter { + def apply[S, A](_value: S, _optic: Setter[S, A]): AppliedSetter[S, A] = + AppliedPSetter(_value, _optic) +} +final case class AppliedPSetterSyntax[S, T, A, B](private val self: AppliedPSetter[S, T, A, B]) extends AnyVal { + + /** compose a [[PSetter]] with a [[PSetter]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + + /** compose a [[PSetter]] with a [[PTraversal]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + + /** compose a [[PSetter]] with a [[POptional]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + + /** compose a [[PSetter]] with a [[PPrism]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + + /** compose a [[PSetter]] with a [[PLens]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + + /** compose a [[PSetter]] with a [[PIso]] */ @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) -} - -object AppliedPSetter { - implicit def appliedSetterSyntax[S, A](self: AppliedSetter[S, A]): AppliedSetterSyntax[S, A] = - new AppliedSetterSyntax(self) + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplySetter */ +/** Extension methods for monomorphic Setter + */ final case class AppliedSetterSyntax[S, A](private val self: AppliedSetter[S, A]) extends AnyVal { - def each[C](implicit evEach: Each[A, C]): AppliedPSetter[S, S, C, C] = + def each[C](implicit evEach: Each[A, C]): AppliedSetter[S, C] = self.andThen(evEach.each) /** Select all the elements which satisfies the predicate. @@ -86,7 +119,7 @@ final case class AppliedSetterSyntax[S, A](private val self: AppliedSetter[S, A] def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedSetter[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedSetter[S, A1] = + def at[I, A1](i: I)(implicit evAt: At[A, I, A1]): AppliedSetter[S, A1] = self.andThen(evAt.at(i)) def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedSetter[S, A1] = diff --git a/core/shared/src/main/scala/monocle/syntax/AppliedPTraversal.scala b/core/shared/src/main/scala/monocle/syntax/AppliedPTraversal.scala index 3187aae6e..ac181dd5e 100644 --- a/core/shared/src/main/scala/monocle/syntax/AppliedPTraversal.scala +++ b/core/shared/src/main/scala/monocle/syntax/AppliedPTraversal.scala @@ -4,89 +4,100 @@ import cats.Applicative import monocle.function.{At, Each, FilterIndex, Index} import monocle._ -final case class AppliedPTraversal[S, T, A, B](s: S, traversal: PTraversal[S, T, A, B]) { - def getAll: List[A] = traversal.getAll(s) - def headOption: Option[A] = traversal.headOption(s) - def lastOption: Option[A] = traversal.lastOption(s) +trait AppliedPTraversal[S, T, A, B] extends AppliedPSetter[S, T, A, B] with AppliedFold[S, A] { + override def optic: PTraversal[S, T, A, B] - def replace(b: B): T = traversal.replace(b)(s) - def modify(f: A => B): T = traversal.modify(f)(s) def modifyA[F[_]: Applicative](f: A => F[B]): F[T] = - traversal.modifyA(f)(s) + optic.modifyA(f)(value) - def find(p: A => Boolean): Option[A] = traversal.find(p)(s) - def exist(p: A => Boolean): Boolean = traversal.exist(p)(s) - def all(p: A => Boolean): Boolean = traversal.all(p)(s) - def isEmpty(s: S): Boolean = traversal.isEmpty(s) - def nonEmpty(s: S): Boolean = traversal.nonEmpty(s) - - /** alias to replace */ - @deprecated("use replace instead", since = "3.0.0-M1") - def set(b: B): T = replace(b) - - def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPTraversal[S, T, A1, B1] = + override def some[A1, B1](implicit ev1: A =:= Option[A1], ev2: B =:= Option[B1]): AppliedPTraversal[S, T, A1, B1] = adapt[Option[A1], Option[B1]].andThen(std.option.pSome[A1, B1]) - private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPTraversal[S, T, A1, B1] = + override private[monocle] def adapt[A1, B1](implicit evA: A =:= A1, evB: B =:= B1): AppliedPTraversal[S, T, A1, B1] = evB.substituteCo[AppliedPTraversal[S, T, A1, *]](evA.substituteCo[AppliedPTraversal[S, T, *, B]](this)) - def andThen[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = - AppliedPSetter(s, traversal.andThen(other)) - def andThen[C](other: Fold[A, C]): AppliedFold[S, C] = - AppliedFold(s, traversal.andThen(other)) def andThen[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, traversal.andThen(other)) - def andThen[C, D](other: POptional[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, traversal.andThen(other)) - def andThen[C, D](other: PPrism[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, traversal.andThen(other)) - def andThen[C, D](other: PLens[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, traversal.andThen(other)) - def andThen[C, D](other: PIso[A, B, C, D]): AppliedPTraversal[S, T, C, D] = - AppliedPTraversal(s, traversal.andThen(other)) + AppliedPTraversal(value, optic.andThen(other)) +} + +object AppliedPTraversal { + def apply[S, T, A, B](_value: S, _optic: PTraversal[S, T, A, B]): AppliedPTraversal[S, T, A, B] = + new AppliedPTraversal[S, T, A, B] { + val value: S = _value + val optic: PTraversal[S, T, A, B] = _optic + } + + implicit def appliedPTraversalSyntax[S, T, A, B]( + self: AppliedPTraversal[S, T, A, B] + ): AppliedPTraversalSyntax[S, T, A, B] = + new AppliedPTraversalSyntax(self) + + implicit def appliedTraversalSyntax[S, A](self: AppliedTraversal[S, A]): AppliedTraversalSyntax[S, A] = + new AppliedTraversalSyntax(self) +} + +object AppliedTraversal { + def apply[S, A](_value: S, _optic: Traversal[S, A]): AppliedTraversal[S, A] = + AppliedPTraversal(_value, _optic) +} + +final case class AppliedPTraversalSyntax[S, T, A, B](private val self: AppliedPTraversal[S, T, A, B]) extends AnyVal { @deprecated("use andThen", since = "3.0.0-M1") - def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = andThen(other) + def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeFold[C](other: Fold[A, C]): AppliedFold[S, C] = andThen(other) + def composeGetter[C](other: Getter[A, C]): AppliedFold[S, C] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeSetter[C, D](other: PSetter[A, B, C, D]): AppliedPSetter[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeTraversal[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeOptional[C, D](other: POptional[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composePrism[C, D](other: PPrism[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) + @deprecated("use andThen", since = "3.0.0-M1") - def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeLens[C, D](other: PLens[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composeTraversal */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def composeIso[C, D](other: PIso[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composeOptional */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def ^|->>[C, D](other: PTraversal[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composePrism */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def ^|-?[C, D](other: POptional[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composeLens */ @deprecated("use andThen", since = "3.0.0-M1") - def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) + def ^<-?[C, D](other: PPrism[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) - /** alias to composeIso */ @deprecated("use andThen", since = "3.0.0-M1") - def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPTraversal[S, T, C, D] = andThen(other) -} + def ^|->[C, D](other: PLens[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) -object AppliedPTraversal { - implicit def appliedTraversalSyntax[S, A](self: AppliedTraversal[S, A]): AppliedTraversalSyntax[S, A] = - new AppliedTraversalSyntax(self) + @deprecated("use andThen", since = "3.0.0-M1") + def ^<->[C, D](other: PIso[A, B, C, D]): AppliedPTraversal[S, T, C, D] = + self.andThen(other) } -/** Extension methods for monomorphic ApplyTraversal */ +/** Extension methods for monomorphic Traversal + */ final case class AppliedTraversalSyntax[S, A](private val self: AppliedTraversal[S, A]) extends AnyVal { def each[C](implicit evEach: Each[A, C]): AppliedTraversal[S, C] = self.andThen(evEach.each) @@ -103,7 +114,7 @@ final case class AppliedTraversalSyntax[S, A](private val self: AppliedTraversal def withDefault[A1](defaultValue: A1)(implicit evOpt: A =:= Option[A1]): AppliedTraversal[S, A1] = self.adapt[Option[A1], Option[A1]].andThen(std.option.withDefault(defaultValue)) - def at[I, A1](i: I)(implicit evAt: At[A, i.type, A1]): AppliedTraversal[S, A1] = + def at[I, A1](i: I)(implicit evAt: At[A, I, A1]): AppliedTraversal[S, A1] = self.andThen(evAt.at(i)) def index[I, A1](i: I)(implicit evIndex: Index[A, I, A1]): AppliedTraversal[S, A1] =