package monocle
import scalaz.{Applicative, Category, Maybe, Monoid, \/}
import scalaz.syntax.std.option._
abstract class PPrism[S, T, A, B] extends Serializable { self =>
def getOrModify(s: S): T \/ A
def reverseGet(b: B): T
def getOption(s: S): Option[A]
@inline final def modifyF[F[_] : Applicative](f: A => F[B])(s: S): F[T] =
getOrModify(s).fold(
t => Applicative[F].point(t),
a => Applicative[F].map(f(a))(reverseGet)
)
@inline final def modify(f: A => B): S => T =
getOrModify(_).fold(identity,a => reverseGet(f(a)))
@inline final def modifyOption(f: A => B): S => Option[T] =
s => getOption(s).map(a => reverseGet(f(a)))
@inline final def set(b: B): S => T =
modify(_ => b)
@inline final def setOption(b: B): S => Option[T] =
modifyOption(_ => b)
@inline final def isMatching(s: S): Boolean =
getOption(s).isDefined
@inline final def re: Getter[B, T] =
Getter(reverseGet)
@inline final def first[C]: PPrism[(S, C), (T, C), (A, C), (B, C)] =
PPrism[(S, C), (T, C), (A, C), (B, C)]{
case (s, c) => getOrModify(s).bimap(_ -> c, _ -> c)
}{
case (b, c) => (reverseGet(b), c)
}
@inline final def second[C]: PPrism[(C, S), (C, T), (C, A), (C, B)] =
PPrism[(C, S), (C, T), (C, A), (C, B)]{
case (c, s) => getOrModify(s).bimap(c -> _, c -> _)
}{
case (c, b) => (c, reverseGet(b))
}
@deprecated("use getOption", since = "1.1.0")
@inline final def getMaybe(s: S): Maybe[A] =
getOption(s).toMaybe
@deprecated("use modifyOption", since = "1.1.0")
@inline final def modifyMaybe(f: A => B): S => Maybe[T] =
s => modifyOption(f)(s).toMaybe
@deprecated("use setOption", since = "1.1.0")
@inline final def setMaybe(b: B): S => Maybe[T] =
s => setOption(b)(s).toMaybe
@inline final def composeFold[C](other: Fold[A, C]): Fold[S, C] =
asFold composeFold other
@inline final def composeGetter[C](other: Getter[A, C]): Fold[S, C] =
asFold composeGetter other
@inline final def composeSetter[C, D](other: PSetter[A, B, C, D]): PSetter[S, T, C, D] =
asSetter composeSetter other
@inline final def composeTraversal[C, D](other: PTraversal[A, B, C, D]): PTraversal[S, T, C, D] =
asTraversal composeTraversal other
@inline final def composeOptional[C, D](other: POptional[A, B, C, D]): POptional[S, T, C, D] =
asOptional composeOptional other
@inline final def composeLens[C, D](other: PLens[A, B, C, D]): POptional[S, T, C, D] =
asOptional composeOptional other.asOptional
@inline final def composePrism[C, D](other: PPrism[A, B, C, D]): PPrism[S, T, C, D] =
new PPrism[S, T, C, D]{
def getOrModify(s: S): T \/ C =
self.getOrModify(s).flatMap(a => other.getOrModify(a).bimap(self.set(_)(s), identity))
def reverseGet(d: D): T =
self.reverseGet(other.reverseGet(d))
def getOption(s: S): Option[C] =
self.getOption(s) flatMap other.getOption
}
@inline final def composeIso[C, D](other: PIso[A, B, C, D]): PPrism[S, T, C, D] =
composePrism(other.asPrism)
@inline final def ^|->>[C, D](other: PTraversal[A, B, C, D]): PTraversal[S, T, C, D] =
composeTraversal(other)
@inline final def ^|-?[C, D](other: POptional[A, B, C, D]): POptional[S, T, C, D] =
composeOptional(other)
@inline final def ^<-?[C, D](other: PPrism[A, B, C, D]): PPrism[S, T, C, D] =
composePrism(other)
@inline final def ^|->[C, D](other: PLens[A, B, C, D]): POptional[S, T, C, D] =
composeLens(other)
@inline final def ^<->[C, D](other: PIso[A, B, C, D]): PPrism[S, T, C, D] =
composeIso(other)
@inline final def asFold: Fold[S, A] = new Fold[S, A]{
def foldMap[M: Monoid](f: A => M)(s: S): M =
getOption(s) map f getOrElse Monoid[M].zero
}
@inline final def asSetter: PSetter[S, T, A, B] =
new PSetter[S, T, A, B]{
def modify(f: A => B): S => T =
self.modify(f)
def set(b: B): S => T =
self.set(b)
}
@inline final def asTraversal: PTraversal[S, T, A, B] =
new PTraversal[S, T, A, B] {
def modifyF[F[_]: Applicative](f: A => F[B])(s: S): F[T] =
self.modifyF(f)(s)
}
@inline final def asOptional: POptional[S, T, A, B] =
new POptional[S, T, A, B]{
def getOrModify(s: S): T \/ A =
self.getOrModify(s)
def set(b: B): S => T =
self.set(b)
def getOption(s: S): Option[A] =
self.getOption(s)
def modify(f: A => B): S => T =
self.modify(f)
def modifyF[F[_]: Applicative](f: A => F[B])(s: S): F[T] =
self.modifyF(f)(s)
}
}
object PPrism extends PrismInstances {
def id[S, T]: PPrism[S, T, S, T] =
PIso.id[S, T].asPrism
def apply[S, T, A, B](_getOrModify: S => T \/ A)(_reverseGet: B => T): PPrism[S, T, A, B] =
new PPrism[S, T, A, B]{
def getOrModify(s: S): T \/ A =
_getOrModify(s)
def reverseGet(b: B): T =
_reverseGet(b)
def getOption(s: S): Option[A] =
_getOrModify(s).toOption
}
}
object Prism {
def id[A]: Prism[A, A] =
Iso.id[A].asPrism
def apply[S, A](_getOption: S => Option[A])(_reverseGet: A => S): Prism[S, A] =
new Prism[S, A]{
def getOrModify(s: S): S \/ A =
_getOption(s).fold[S \/ A](\/.left(s))(\/.right)
def reverseGet(b: A): S =
_reverseGet(b)
def getOption(s: S): Option[A] =
_getOption(s)
}
}
sealed abstract class PrismInstances {
implicit val prismCategory: Category[Prism] = new Category[Prism] {
def id[A]: Prism[A, A] =
Prism.id
def compose[A, B, C](f: Prism[B, C], g: Prism[A, B]): Prism[A, C] =
g composePrism f
}
}