Skip to content

Instantly share code, notes, and snippets.

@MateuszKubuszok
Last active January 14, 2022 15:36
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 MateuszKubuszok/1f68adf47c85ea46ab702f6e8caad8c2 to your computer and use it in GitHub Desktop.
Save MateuszKubuszok/1f68adf47c85ea46ab702f6e8caad8c2 to your computer and use it in GitHub Desktop.
type ~>[F[_], G[_]] = [A] => F[A] => G[A]
final case class <~>[F[_], G[_]](to: F ~> G, from: G ~> F):
val mapAsTo: [A, B] => (G[A] => G[B]) => (F[A] => F[B]) =
[A, B] => (f: (G[A] => G[B])) => to[A].andThen(f).andThen(from[B])
val mapAsFrom: [A, B] => (F[A] => F[B]) => (G[A] => G[B]) =
[A, B] => (f: (F[A] => F[B])) => from[A].andThen(f).andThen(to[B])
extension [F[_], A](fa: F[A])
transparent inline def mapAs[G[_]]: [B] => (G[A] => G[B]) => F[B] =
scala.compiletime.summonFrom {
case biK: (F <~> G) => [B] => (f: (G[A] => G[B])) => biK.mapAsTo[A, B](f)(fa)
case biK: (G <~> F) => [B] => (f: (G[A] => G[B])) => biK.mapAsFrom[A, B](f)(fa)
}
// idea: (fa: F[A]).mapAs[G] { ga => g... : G[B] } : F[B]
@MateuszKubuszok
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment