Skip to content

Commit 00c5309

Browse files
author
sujithjay
committed
Change formatting
1 parent 24ba9b5 commit 00c5309

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

_tour/multiple-parameter-lists.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,55 @@ 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 [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).
1717

1818
Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections:
1919

2020
```
2121
def foldLeft[B](z: B)(op: (B, A) => B): B
2222
```
2323

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.
2527

2628
```tut
2729
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
2830
val res = numbers.foldLeft(0)((m, n) => m + n)
2931
print(res) // 55
3032
```
3133

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-
3434
Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include:
3535

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

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.
4644

47-
Also, it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below:
48-
```tut
49-
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
50-
val numberFunc = numbers.foldLeft(List[Int]())_
51-
52-
val squares = numberFunc((xs, x) => xs:+ x*x)
53-
print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
45+
```
46+
numbers.foldLeft(0)(_ + _)
47+
```
5448

55-
val cubes = numberFunc((xs, x) => xs:+ x*x*x)
56-
print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
57-
```
49+
Also, it allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below:
50+
```tut
51+
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
52+
val numberFunc = numbers.foldLeft(List[Int]())_
5853
59-
2. ##### Implicit parameters
60-
To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is:
54+
val squares = numberFunc((xs, x) => xs:+ x*x)
55+
print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
56+
57+
val cubes = numberFunc((xs, x) => xs:+ x*x*x)
58+
print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000)
59+
```
6160

62-
```
63-
def execute(arg: Int)(implicit ec: ExecutionContext) = ???
64-
```
61+
#### Implicit parameters
62+
To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is:
63+
64+
```
65+
def execute(arg: Int)(implicit ec: ExecutionContext) = ???
66+
```
6567

0 commit comments

Comments
 (0)