Skip to content

Commit 4afecb3

Browse files
SethTisuejvican
authored andcommitted
language tour: overhaul multiple parameter lists section (#1288)
1 parent 6745e66 commit 4afecb3

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

_tour/multiple-parameter-lists.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ previous-page: nested-functions
1313
redirect_from: "/tutorials/tour/multiple-parameter-lists.html"
1414
---
1515

16-
Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. This is formally known as [partial application](https://en.wikipedia.org/wiki/Partial_application).
16+
Methods may have multiple parameter lists.
1717

18-
Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections:
18+
### Example
19+
20+
Here is an example, as defined on the `TraversableOnce` trait in Scala's collections API:
1921

2022
```
2123
def foldLeft[B](z: B)(op: (B, A) => B): B
2224
```
2325

24-
`foldLeft` applies a binary operator `op` to an initial value `z` and all elements of this traversable, going left to right. Shown below is an example of its usage.
26+
`foldLeft` applies a two-parameter function `op` to an initial value `z` and all elements of this collection, going left to right. Shown below is an example of its usage.
2527

2628
Starting with an initial value of 0, `foldLeft` here applies the function `(m, n) => m + n` to each element in the List and the previous accumulated value.
2729

@@ -33,51 +35,49 @@ println(res) // 55
3335
```
3436
{% endscalafiddle %}
3537

36-
Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include:
38+
### Use cases
39+
40+
Suggested use cases for multiple parameter lists include:
3741

3842
#### Single functional parameter
39-
In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. Without multiple parameter lists, the code would look like this:
43+
44+
In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. Without multiple parameter lists, the code would look like this:
4045

4146
```
42-
numbers.foldLeft(0, {(m: Int, n: Int) => m + n})
47+
numbers.foldLeft(0, (m: Int, n: Int) => m + n)
4348
```
44-
45-
Note that the use of multiple parameter lists here also allows us to take advantage of Scala type inference to make the code more concise as shown below; which would not be possible in a non-curried definition.
46-
49+
50+
Note that the use of multiple parameter lists here also allows us to take advantage of Scala type inference to make the code more concise, like this:
51+
4752
```
4853
numbers.foldLeft(0)(_ + _)
4954
```
50-
Also it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below:
51-
```tut
52-
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
53-
val numberFunc = numbers.foldLeft(List[Int]())_
5455

55-
val squares = numberFunc((xs, x) => xs:+ x*x)
56-
print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
56+
this would not be possible with only a single parameter list, as the Scala compiler would not be able to infer the parameter types of the function.
57+
58+
#### Implicit parameters
59+
60+
To specify only certain parameters as `implicit`, they must be placed in their own `implicit` parameter list.
61+
62+
An example of this is:
5763

58-
val cubes = numberFunc((xs, x) => xs:+ x*x*x)
59-
print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
64+
```
65+
def execute(arg: Int)(implicit ec: scala.concurrent.ExecutionContext) = ???
6066
```
6167

62-
Finally, `foldLeft` and `foldRight` can be used in any of the following terms,
63-
```tut
64-
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
68+
#### Partial application
6569

66-
numbers.foldLeft(0)((sum, item) => sum + item) // Generic Form
67-
numbers.foldRight(0)((sum, item) => sum + item) // Generic Form
70+
When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. This is formally known as [partial application](https://en.wikipedia.org/wiki/Partial_application).
6871

69-
numbers.foldLeft(0)(_+_) // Curried Form
70-
numbers.foldRight(0)(_+_) // Curried Form
72+
For example,
7173

72-
(0 /: numbers)(_+_) // Used in place of foldLeft
73-
(numbers :\ 0)(_+_) // Used in place of foldRight
74-
```
74+
```tut
75+
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
76+
val numberFunc = numbers.foldLeft(List[Int]()) _
7577
76-
77-
#### Implicit parameters
78-
To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is:
78+
val squares = numberFunc((xs, x) => xs :+ x*x)
79+
print(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
7980
81+
val cubes = numberFunc((xs, x) => xs :+ x*x*x)
82+
print(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
8083
```
81-
def execute(arg: Int)(implicit ec: ExecutionContext) = ???
82-
```
83-

0 commit comments

Comments
 (0)