Skip to content

Instantly share code, notes, and snippets.

@harrah
Created June 2, 2010 19:39
Show Gist options
  • Save harrah/422874 to your computer and use it in GitHub Desktop.
Save harrah/422874 to your computer and use it in GitHub Desktop.
trait ~>[A[_], B[_]] { def apply[T](a: A[T]): B[T] }
object Test {
def x[A[_],B[_]](f: A ~> B) = f
import Param._
val f = x { (p: Param[Option,List]) => p.ret( p.in.toList ) }
val a: List[Int] = f( Some(3) )
val b: List[String] = f( Some("aa") )
}
trait Param[A[_], B[_]] {
type T
def in: A[T]
def ret(out: B[T])
def ret: B[T]
}
object Param {
implicit def pToT[A[_], B[_]](p: Param[A,B] => Unit): A~>B = new (A ~> B) {
def apply[s](a: A[s]): B[s] = {
val v: Param[A,B] { type T = s} = new Param[A,B] { type T = s
def in = a
private var r: B[T] = _
def ret(b: B[T]) {r = b}
def ret: B[T] = r
}
p(v)
v.ret
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment