You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
17
17
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:
19
21
20
22
```
21
23
def foldLeft[B](z: B)(op: (B, A) => B): B
22
24
```
23
25
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.
25
27
26
28
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.
27
29
@@ -33,51 +35,49 @@ println(res) // 55
33
35
```
34
36
{% endscalafiddle %}
35
37
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:
37
41
38
42
#### 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:
40
45
41
46
```
42
-
numbers.foldLeft(0, {(m: Int, n: Int) => m + n})
47
+
numbers.foldLeft(0, (m: Int, n: Int) => m + n)
43
48
```
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
+
47
52
```
48
53
numbers.foldLeft(0)(_ + _)
49
54
```
50
-
Also it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below:
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
65
69
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).
68
71
69
-
numbers.foldLeft(0)(_+_) // Curried Form
70
-
numbers.foldRight(0)(_+_) // Curried Form
72
+
For example,
71
73
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]()) _
75
77
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:
0 commit comments