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
Copy file name to clipboardExpand all lines: _tour/multiple-parameter-lists.md
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -24,13 +24,13 @@ def foldLeft[B](z: B)(op: (B, A) => B): B
24
24
25
25
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.
26
26
27
-
{% scalafiddle %}
27
+
{% Scalafiddle %}
28
28
```tut
29
29
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
30
30
val res = numbers.foldLeft(0)((m, n) => m + n)
31
31
println(res) // 55
32
32
```
33
-
{% endscalafiddle %}
33
+
{% endScalafiddle %}
34
34
35
35
### Use cases
36
36
@@ -39,33 +39,33 @@ Suggested use cases for multiple parameter lists include:
39
39
#### Drive type inference
40
40
41
41
It so happens that in Scala, type inference proceeds one parameter list at at time.
42
-
Say, you have the following method:
42
+
Say you have the following method:
43
43
44
44
```tut
45
-
def foldleft1[A, B](as: List[A], b0: B, op: (B, A) => B) = ???
45
+
def foldLeft1[A, B](as: List[A], b0: B, op: (B, A) => B) = ???
46
46
```
47
47
48
48
Then you'd like to call it in the following way, but will find that it doesn't compile:
49
49
50
50
```tut:fail
51
-
def notpossible = foldleft1(numbers, 0, _ + _)
51
+
def notPossible = foldLeft1(numbers, 0, _ + _)
52
52
```
53
53
54
54
you will have to call it like one of the below ways:
def secondWay = foldLeft1(numbers, 0, (a: Int, b: Int) => a + b)
59
59
```
60
60
61
-
That's because scala won't be able to infer the type of the function `_ + _`, as it's still inferring `A` and `B`. By moving the parameter `op` to its own parameter list, `A` and `B` are inferred in the first parameter list. These inferred types will then be available to the second parameter list and `_ + _` will match the the inferred type `(Int, Int) => Int`
61
+
That's because Scala won't be able to infer the type of the function `_ + _`, as it's still inferring `A` and `B`. By moving the parameter `op` to its own parameter list, `A` and `B` are inferred in the first parameter list. These inferred types will then be available to the second parameter list and `_ + _` will match the the inferred type `(Int, Int) => Int`
62
62
63
63
```tut
64
-
def foldleft2[A, B](as: List[A], b0: B)(op: (B, A) => B) = ???
65
-
def possible = foldleft2(numbers, 0)(_ + _)
64
+
def foldLeft2[A, B](as: List[A], b0: B)(op: (B, A) => B) = ???
65
+
def possible = foldLeft2(numbers, 0)(_ + _)
66
66
```
67
67
68
-
This definition doesn't need any type hints and can infer all of its parameters.
68
+
This definition doesn't need any type hints and can infer all of its type parameters.
69
69
70
70
71
71
#### Implicit parameters
@@ -74,7 +74,7 @@ To specify only certain parameters as `implicit`, they must be placed in their o
0 commit comments