Skip to content

language tour: Multiple Parameter Lists: clarify relationship to 'currying' #2490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion _tour/multiple-parameter-lists.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tour
title: Multiple Parameter Lists (Currying)
title: Multiple Parameter Lists
partof: scala-tour

num: 12
Expand Down Expand Up @@ -96,3 +96,41 @@ 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.

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
```