Skip to content

Instantly share code, notes, and snippets.

@mrvisser
Last active August 29, 2015 14:05
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 mrvisser/a7f0eb4201bbbc009135 to your computer and use it in GitHub Desktop.
Save mrvisser/a7f0eb4201bbbc009135 to your computer and use it in GitHub Desktop.
object Main {
trait ~>[F[_], G[_]] {
def apply[T](t: F[T]): G[T]
}
// ERROR: Compiler doesn't think this implements `def apply[T](t: F[T]): G[T]` :(
class Poly1[F[_], G[_]] extends ~> {
private def implicitApply[T](t: F[T])(implicit f: F[T] ~> G[T]): G[T] = f(t)
def apply[T](t: F[T]): G[T] = implicitApply(t)
}
def main(args: Array[String]): Unit = {
type Id[T] = T
type Const[C] = {
type I[T] = C
}
def singleton = new (Id ~> Set) {
def apply[T](t: T): Set[T] = Set(t)
}
def identity = new (Id ~> Id) {
def apply[T](t: T): T = t
}
def headOption = new (List ~> Option) {
def apply[T](t: List[T]): Option[T] = t.headOption
}
def size = new (Id ~> Const[Int]#I) {
def apply[T](t: T): Int = 0
}
println(singleton(List(1, 2, 3)))
println(singleton("7"))
println(identity(List(1, 2, 3)))
println(identity("7"))
println(headOption(List(1, 2, 3)))
println(size(List(1, 2, 3)))
println(size("7"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment