Skip to content

Instantly share code, notes, and snippets.

@globulon
Created November 30, 2018 15:20
Show Gist options
  • Select an option

  • Save globulon/42d5780789bcd7bf45e56be3ce983715 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/42d5780789bcd7bf45e56be3ce983715 to your computer and use it in GitHub Desktop.
trait Isomorphism[Arrow[_, _], A, B] {
def to: Arrow[A, B]
def from: Arrow[B, A]
}
trait Isomorphisms {
type IsoSet[A, B] = Isomorphism[Function1, A, B]
type <=>[A, B] = IsoSet[A, B]
implicit def eitherIsoSafe[A]: Either[List[Throwable], A] <=> Safe[A] =
new (Either[List[Throwable], A] <=> Safe[A]) {
override def to: Either[scala.List[scala.Throwable], A] => Safe[A] = {
case Right(a) => safe(a)
case Left(exs) => Invalid(exs)
}
override def from: Safe[A] => Either[List[Throwable], A] = {
case Valid(a) => Right(a)
case Invalid(exs) => Left(exs)
}
}
trait IsoSetOps[A, B] {
def iso: A <=> B
def self: A
def asT[C](implicit eq: B === C): C = eq.coerce(iso.to(self))
}
trait ToIsoSetOps {
implicit def toIsoSetOps[A, B](target: A)(implicit tc: A <=> B): IsoSetOps[A, B] =
new IsoSetOps[A, B] {
val self: A = target
val iso: A <=> B = tc
}
}
object iso extends ToIsoSetOps
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment