From 0dcda02b6f6bfe528b6e017a4674d371039ec696 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Mon, 15 Aug 2022 13:54:47 -0400 Subject: [PATCH 1/2] language tour: Multiple Parameter Lists: clarify relationship to 'currying' --- _tour/multiple-parameter-lists.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/_tour/multiple-parameter-lists.md b/_tour/multiple-parameter-lists.md index 034a1be341..f12dbabb12 100644 --- a/_tour/multiple-parameter-lists.md +++ b/_tour/multiple-parameter-lists.md @@ -1,6 +1,6 @@ --- layout: tour -title: Multiple Parameter Lists (Currying) +title: Multiple Parameter Lists partof: scala-tour num: 12 @@ -96,3 +96,23 @@ println(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) val cubes = numberFunc((xs, x) => xs :+ x*x*x) println(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) ``` + +### Comparison with "currying" + +You may sometimes see a method with multiple parameter lists referred to as "curried". + +As the [Wikipedia article on currying](https://en.wikipedia.org/wiki/Currying) states, + +> Currying is the technique of converting a function that takes +> multiple arguments into a sequence of functions that each takes a +> single argument + +We discourage the use of the word "curry" in reference to Scala's multiple parameter lists, for two reasons: + +1) In Scala, multiple parameters and multiple parameter lists are +specified and implemented directly, as part of the language, rather +being derived from single-parameter functions. + +2) There is danger of confusion with the Scala standard library's +[`curried`](https://www.scala-lang.org/api/current/scala/Function2.html#curried:T1=%3E(T2=%3ER)) +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. From 292b84f34332da44a46ed3d5f108ffb973236879 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 16 Aug 2022 14:32:02 -0400 Subject: [PATCH 2/2] language tour: multiple parameter lists: more on currying --- _tour/multiple-parameter-lists.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/_tour/multiple-parameter-lists.md b/_tour/multiple-parameter-lists.md index f12dbabb12..6bb46d420c 100644 --- a/_tour/multiple-parameter-lists.md +++ b/_tour/multiple-parameter-lists.md @@ -116,3 +116,21 @@ being derived from single-parameter functions. 2) There is danger of confusion with the Scala standard library's [`curried`](https://www.scala-lang.org/api/current/scala/Function2.html#curried:T1=%3E(T2=%3ER)) 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. + +Regardless, there are certainly similarities to be found between +multiple parameter lists and currying. Though they are different at +the definition site, the call site might nonetheless look identical, +as in this example: + +```scala mdoc +// version with multiple parameter lists +def addMultiple(n1: Int)(n2: Int) = n1 + n2 +// two different ways of arriving at a curried version instead +def add(n1: Int, n2: Int) = n1 + n2 +val addCurried1 = (add _).curried +val addCurried2 = (n1: Int) => (n2: Int) => n1 + n2 +// regardless, all three call sites are identical +addMultiple(3)(4) // 7 +addCurried1(3)(4) // 7 +addCurried2(3)(4) // 7 +```