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
`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.
28
43
29
44
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.
30
45
46
+
{% tabs foldLeft_use %}
47
+
48
+
{% tab 'Scala 2 and 3' for=foldLeft_use %}
31
49
```scala mdoc
32
50
valnumbers=List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
33
51
valres= numbers.foldLeft(0)((m, n) => m + n)
34
52
println(res) // 55
35
53
```
54
+
{% endtab %}
55
+
56
+
{% endtabs %}
36
57
37
58
### Use cases
38
59
@@ -43,29 +64,53 @@ Suggested use cases for multiple parameter lists include:
43
64
It so happens that in Scala, type inference proceeds one parameter list at a time.
44
65
Say you have the following method:
45
66
67
+
{% tabs foldLeft1_definition %}
68
+
69
+
{% tab 'Scala 2 and 3' for=foldLeft1_definition %}
46
70
```scala mdoc
47
71
deffoldLeft1[A, B](as: List[A], b0: B, op: (B, A) =>B) =???
48
72
```
73
+
{% endtab %}
74
+
75
+
{% endtabs %}
49
76
50
77
Then you'd like to call it in the following way, but will find that it doesn't compile:
51
78
79
+
{% tabs foldLeft1_wrong_use %}
80
+
81
+
{% tab 'Scala 2 and 3' for=foldLeft1_wrong_use %}
52
82
```scala mdoc:fail
53
83
defnotPossible= foldLeft1(numbers, 0, _ + _)
54
84
```
85
+
{% endtab %}
86
+
87
+
{% endtabs %}
55
88
56
89
you will have to call it like one of the below ways:
defsecondWay= foldLeft1(numbers, 0, (a: Int, b: Int) => a + b)
61
97
```
98
+
{% endtab %}
99
+
100
+
{% endtabs %}
62
101
63
102
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 inferred type `(Int, Int) => Int`
64
103
104
+
{% tabs foldLeft2_definition_and_use %}
105
+
106
+
{% tab 'Scala 2 and 3' for=foldLeft2_definition_and_use %}
65
107
```scala mdoc
66
108
deffoldLeft2[A, B](as: List[A], b0: B)(op: (B, A) =>B) =???
67
109
defpossible= foldLeft2(numbers, 0)(_ + _)
68
110
```
111
+
{% endtab %}
112
+
113
+
{% endtabs %}
69
114
70
115
This definition doesn't need any type hints and can infer all of its type parameters.
71
116
@@ -76,16 +121,31 @@ To specify only certain parameters as [`implicit`](https://docs.scala-lang.org/t
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).
0 commit comments