Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Last active November 11, 2023 05:22
Show Gist options
  • Save gigiigig/3cd104e8951b4432afd5 to your computer and use it in GitHub Desktop.
Save gigiigig/3cd104e8951b4432afd5 to your computer and use it in GitHub Desktop.
Aux Pattern
import shapeless._
import scalaz._
import Scalaz._
object console extends App {
trait Foo[A] {
type B
def value: B
}
object Foo {
type Aux[A0, B0] = Foo[A0] { type B = B0 }
implicit def fi = new Foo[Int] {
type B = String
val value = "Foo"
}
implicit def fs = new Foo[String] {
type B = Boolean
val value = false
}
}
def ciao[T, R](t: T)
(implicit f: Foo.Aux[T, R],
m: Monoid[R]): R = f.value
val res = ciao(2)
println(s"res: ${res}")
}
import scalaz._
import Scalaz._
object console extends App {
trait Apart[F]{
type T
type W[X]
def apply(f: F): W[T]
}
object Apart {
def apply[F](implicit apart: Apart[F]) = apart
type Aux[FA, F[_], A] = Apart[FA]{ type T = A; type W[X] = F[X] }
implicit def mk[F[_], R]: Aux[F[R], F, R] = new Apart[F[R]]{
type T = R
type W[X] = F[X]
def apply(f: F[R]): W[T] = f
}
}
def mapZero[Thing, F[_], A](thing: Thing)
(implicit apart: Apart.Aux[Thing, F, A],
f: Functor[F],
m: Monoid[A]): F[A] =
f.map(apart(thing))(_ => m.zero)
mapZero(Option(3))
}
@MartinSeeler
Copy link

Great example, thank you!

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