Skip to content

Instantly share code, notes, and snippets.

@lambder
Forked from gigiigig/auxpattern.scala
Created December 30, 2016 17:57
Show Gist options
  • Save lambder/e4e8f3aeecf3ed1d4ff364dd40593f67 to your computer and use it in GitHub Desktop.
Save lambder/e4e8f3aeecf3ed1d4ff364dd40593f67 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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment