Skip to content

Instantly share code, notes, and snippets.

@anarcher
Created August 18, 2010 05:05
Show Gist options
  • Save anarcher/533509 to your computer and use it in GitHub Desktop.
Save anarcher/533509 to your computer and use it in GitHub Desktop.
Scala Currying & By-name parameter (function value)
scala> def a(x : => Unit) { print("a") }
a: (x: => Unit)Unit
scala> a {
| print("b")
| }
scala> def a(x:Int)(y : => Int) {
| print(x+y)
| }
a: (x: Int)(y: => Int)Unit
scala> val b = a(10)_
b: (=> Int) => Unit = <function1>
scala> b { 20 }
30
// -----------------------------------------------------------------------
scala> def a(x:Int) = (y:Int) => print(x+y)
a: (x: Int)(Int) => Unit
scala> val b = a(10)
b: (Int) => Unit = <function1>
scala> b(20)
30
// -----------------------------------------------------------------------
scala> def a(x:Int)(y:Int)(z:Int) { print(x+y+z) }
scala> val b = a _
b: (Int) => (Int) => (Int) => Unit = <function1>
scala> val c = b(10)
c: (Int) => (Int) => Unit = <function1>
scala> val e = c(20)
e: (Int) => Unit = <function1>
scala> val f = e(30)
60f: Unit = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment