-
-
Notifications
You must be signed in to change notification settings - Fork 202
/
These.scala
37 lines (29 loc) · 1.12 KB
/
These.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package monocle.std
import monocle.Optional
import monocle.POptional
import monocle.Prism
import cats.data.Ior.{Both, Left => This, Right => That}
import cats.data.Ior
import cats.syntax.either._
object these extends TheseOptics
trait TheseOptics {
def theseToDisjunction[A, B]: Prism[Ior[A, B], Either[A, B]] =
Prism[Ior[A, B], Either[A, B]] {
case This(a) => Some(a.asLeft[B])
case That(b) => Some(b.asRight[A])
case Both(_, _) => None
} {
case Left(a) => This(a)
case Right(b) => That(b)
}
def pTheseLeft[A, B, C]: POptional[Ior[A, B], Ior[C, B], A, C] =
POptional[Ior[A, B], Ior[C, B], A, C](ior => ior.fold(a => Right(a), b => Left(Ior.right(b)), (a, _) => Right(a)))(
c => ior => ior.leftMap(_ => c)
)
def theseLeft[A, B]: Optional[Ior[A, B], A] = pTheseLeft[A, B, A]
def pTheseRight[A, B, C]: POptional[Ior[A, B], Ior[A, C], B, C] =
POptional[Ior[A, B], Ior[A, C], B, C](ior => ior.fold(a => Left(Ior.left(a)), b => Right(b), (_, b) => Right(b)))(
c => ior => ior.map(_ => c)
)
def theseRight[A, B]: Optional[Ior[A, B], B] = pTheseRight[A, B, B]
}