Created
November 20, 2009 05:17
-
-
Save sadache/239302 to your computer and use it in GitHub Desktop.
default-currying-lifting-idea-in.scala The idea deserves maybe writing a whole paper about. But this illustrates the basic concept.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Test { | |
implicit def lift0[A,B >: Any](a:A):B=>A=_=>a | |
implicit def lift1[A>: Any,B>: Any,C >: Any](f:A=>B):(C=>A)=>C=>A=>B = h=>c =>f (h(c)) | |
implicit def lift2[A<:Any,B<:Any,C<:Any,D >: scala.Nothing <:scala.Any](f:Function2[A,B,C]):Function3[Function1[D,A],Function1[D,B],D,C] =( h ,g, d)=>f (h(d),g(d)) | |
implicit def curry2[a1 >: scala.Nothing <: scala.Any, a2 >: scala.Nothing <: scala.Any, b >: scala.Nothing <: scala.Any] (f:Function2[a1,a2,b]): scala.Function1[a1, scala.Function1[a2, b]] =Function.curried(f) | |
implicit def curry3[a1 >: scala.Nothing <: scala.Any, a2 >: scala.Nothing <: scala.Any, a3 >: scala.Nothing <: scala.Any, b >: scala.Nothing <: scala.Any] (f:Function3[a1,a2,a3,b]): scala.Function1[a1, scala.Function1[a2,scala.Function1[a3,b]]] =Function.curried(f) | |
//implicit def self[A](a:A):A=a | |
def main(args:Array[String]){ | |
val x2:Int=>Int=i => i*2 | |
val minus2:Int=>Int=i => i-2 | |
val plus:(Int,Int)=>Int= (i,j)=>i+j | |
println(plus (1)(2)) // 3 | |
println(plus(x2,minus2,2)) // 4 | |
println(plus(x2)(minus2)(2)(3)) // 5 | |
println(1(1)(1)(1)(1)(2)(4)(7)(2)(4)(7)(2)(4)(7)) // 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment