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 [currying](https://en.wikipedia.org/wiki/Currying)
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 [currying](https://en.wikipedia.org/wiki/Currying).
17
17
18
18
Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections:
19
19
20
20
```
21
21
def foldLeft[B](z: B)(op: (B, A) => B): B
22
22
```
23
23
24
-
`foldLeft` applies a binary operator `op` to an initial value `z` and all elements of this traversable, going left to right. Here is an example of its usage:
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.
25
+
26
+
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.
25
27
26
28
```tut
27
29
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
28
30
val res = numbers.foldLeft(0)((m, n) => m + n)
29
31
print(res) // 55
30
32
```
31
33
32
-
Starting with an initial value of 0, `foldLeft` applies the function `(m, n) => m + n` to each element in the List and the previous accumulated value.
33
-
34
34
Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include:
35
35
36
-
1.##### Single functional parameter
37
-
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:
38
-
```
39
-
numbers.foldLeft(0, {(m: Int, n: Int) => m + n})
40
-
```
41
-
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 for a non-curried definition.
36
+
#### Single functional parameter
37
+
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:
38
+
39
+
```
40
+
numbers.foldLeft(0, {(m: Int, n: Int) => m + n})
41
+
```
42
42
43
-
```
44
-
numbers.foldLeft(0)(_ + _)
45
-
```
43
+
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
44
47
-
Also, it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below:
0 commit comments