Created
September 27, 2018 04:50
-
-
Save beezee/fe8afcff7786ca355b760d4d04cc6ed8 to your computer and use it in GitHub Desktop.
what does it mean?
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
// List[Either[A, B]] <=> (List[A], List[B]) | |
// Option[These[A, B]] <=> (Option[A], Option[B]) | |
// what does it mean that this compiles? what can be derived as a relationship? what can be induced? | |
trait Iso[A, B] { | |
def from(a: A): B | |
def to(b: B): A | |
} | |
sealed trait These[A, B] | |
case class This[A, B](run: A) extends These[A, B] | |
case class That[A, B](run: B) extends These[A, B] | |
case class Both[A, B](run: (A, B)) extends These[A, B] | |
object Alg { | |
def CopProd[A, B]: Iso[List[Either[A, B]], (List[A], List[B])] = | |
new Iso[List[Either[A, B]], (List[A], List[B])] { | |
def from(a: List[Either[A, B]]): (List[A], List[B]) = | |
a.foldLeft((List.empty[A], List.empty[B]))((acc, el) => | |
el match { | |
case Left(l) => (acc._1 :+ l, acc._2) | |
case Right(r) => (acc._1, acc._2 :+ r) | |
}) | |
def to(b: (List[A], List[B])): List[Either[A, B]] = | |
b._1.map(Left(_)) ++ b._2.map(Right(_)) | |
} | |
def OpsThese[A, B]: Iso[Option[These[A, B]], (Option[A], Option[B])] = | |
new Iso[Option[These[A, B]], (Option[A], Option[B])] { | |
def from(a: Option[These[A, B]]): (Option[A], Option[B]) = | |
a match { | |
case None => (None, None) | |
case Some(x) => x match { | |
case This(a) => (Some(a), None) | |
case That(b) => (None, Some(b)) | |
case Both((a, b)) => (Some(a), Some(b)) | |
} | |
} | |
def to(b: (Option[A], Option[B])): Option[These[A, B]] = | |
b match { | |
case (None, None) => None | |
case (Some(x), None) => Some(This(x)) | |
case (None, Some(y)) => Some(That(y)) | |
case (Some(x), Some(y)) => Some(Both((x, y))) | |
} | |
} | |
} |
List, Either, (List, List)
Option, These, (Option, Option)
only true if List is changed to SortedSet
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
F[G[A, B]] <=> H[F[A], F[B]] for what (F, G, H)