-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add inheritance for applied optics (#1116)
- Loading branch information
1 parent
d1c1746
commit 002990e
Showing
9 changed files
with
551 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 72 additions & 47 deletions
119
core/shared/src/main/scala/monocle/syntax/AppliedGetter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
Oops, something went wrong.