|
1 | 1 | ---
|
2 | 2 | layout: tour
|
3 |
| -title: Multiple Parameter Lists (Currying) |
| 3 | +title: Multiple Parameter Lists |
4 | 4 | partof: scala-tour
|
5 | 5 |
|
6 | 6 | num: 12
|
@@ -96,3 +96,41 @@ println(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
|
96 | 96 | val cubes = numberFunc((xs, x) => xs :+ x*x*x)
|
97 | 97 | println(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
|
98 | 98 | ```
|
| 99 | + |
| 100 | +### Comparison with "currying" |
| 101 | + |
| 102 | +You may sometimes see a method with multiple parameter lists referred to as "curried". |
| 103 | + |
| 104 | +As the [Wikipedia article on currying](https://en.wikipedia.org/wiki/Currying) states, |
| 105 | + |
| 106 | +> Currying is the technique of converting a function that takes |
| 107 | +> multiple arguments into a sequence of functions that each takes a |
| 108 | +> single argument |
| 109 | +
|
| 110 | +We discourage the use of the word "curry" in reference to Scala's multiple parameter lists, for two reasons: |
| 111 | + |
| 112 | +1) In Scala, multiple parameters and multiple parameter lists are |
| 113 | +specified and implemented directly, as part of the language, rather |
| 114 | +being derived from single-parameter functions. |
| 115 | + |
| 116 | +2) There is danger of confusion with the Scala standard library's |
| 117 | +[`curried`](https://www.scala-lang.org/api/current/scala/Function2.html#curried:T1=%3E(T2=%3ER)) |
| 118 | +and [`uncurried`](https://www.scala-lang.org/api/current/scala/Function$.html#uncurried[T1,T2,R](f:T1=%3E(T2=%3ER)):(T1,T2)=%3ER) methods, which don't involve multiple parameter lists at all. |
| 119 | + |
| 120 | +Regardless, there are certainly similarities to be found between |
| 121 | +multiple parameter lists and currying. Though they are different at |
| 122 | +the definition site, the call site might nonetheless look identical, |
| 123 | +as in this example: |
| 124 | + |
| 125 | +```scala mdoc |
| 126 | +// version with multiple parameter lists |
| 127 | +def addMultiple(n1: Int)(n2: Int) = n1 + n2 |
| 128 | +// two different ways of arriving at a curried version instead |
| 129 | +def add(n1: Int, n2: Int) = n1 + n2 |
| 130 | +val addCurried1 = (add _).curried |
| 131 | +val addCurried2 = (n1: Int) => (n2: Int) => n1 + n2 |
| 132 | +// regardless, all three call sites are identical |
| 133 | +addMultiple(3)(4) // 7 |
| 134 | +addCurried1(3)(4) // 7 |
| 135 | +addCurried2(3)(4) // 7 |
| 136 | +``` |
0 commit comments