Skip to content

Instantly share code, notes, and snippets.

@beezee
Created September 27, 2018 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beezee/fe8afcff7786ca355b760d4d04cc6ed8 to your computer and use it in GitHub Desktop.
Save beezee/fe8afcff7786ca355b760d4d04cc6ed8 to your computer and use it in GitHub Desktop.
what does it mean?
// 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)))
}
}
}
@beezee
Copy link
Author

beezee commented Sep 27, 2018

F[G[A, B]] <=> H[F[A], F[B]] for what (F, G, H)

@beezee
Copy link
Author

beezee commented Sep 27, 2018

List, Either, (List, List)
Option, These, (Option, Option)

@beezee
Copy link
Author

beezee commented Sep 27, 2018

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