Scala List foldLeft and foldRight
The Scala List class introduces foldLeft and foldRight which will be unfamiliar to Java developers. The fold methods apply a function to each element in the list either starting at the first element or the last, for foldLeft and foldRight respectfully. The result of the function applied to the first element is passed to the second function. Clear as mud? Let me explain with an example
Create a List and then apply the foldLeft function to it.
val sentence = List("Mary", "had", "a", "little", "lamb")
sentence.foldLeft("start")((a,b) => a + b)
The result of the code above is a String startMaryhadalittlelamb. The Scala doc for the List class the foldLeft method is override def foldLeft[B](z : B)(f : (B, A) => B) : B. The function (B, A) => B gets called for each element in the list where the first parameter is the element from the list and the second parameter is z for the first element in the list and the result of the (B, A) => B function on the previous element on the list for subsequent ones. Adding some debug to the code shows this in action.
val sentence = List("Mary", "had", "a", "little", "lamb")
sentence.foldLeft("start")((a,b) => {
println("[a:" + a + "][b:"+ b + "]");
a + b
})
[a:start][b:Mary]
[a:startMary][b:had]
[a:startMaryhad][b:a]
[a:startMaryhada][b:little]
[a:startMaryhadalittle][b:lamb]
What about foldRight? Changing the code to example above to use foldRight looks like this.
val sentence = List("Mary", "had", "a", "little", "lamb")
sentence.foldRight("start")((a,b) => a + b)
This gives the result Maryhadalittlelambstart. The difference between this result and the foldLeft example is that the initial value start is on the end of the String now rather than the beginning. The API docs for the List class say that foldRight Combines the elements of this list together using the binary function f, from right to left, and starting with the value z.
so I expected the result to be startlamblittleahadMary. Running the code with the debug statements in shows the reason for the difference between the actual and my expected results.
val sentence = List("Mary", "had", "a", "little", "lamb")
sentence.foldRight("start")((a,b) => {
println("[a:" + a + "][b:"+ b + "]");
a + b
})
[a:lamb][b:start]
[a:little][b:lambstart]
[a:a][b:littlelambstart]
[a:had][b:alittlelambstart]
[a:Mary][b:hadalittlelambstart]
Where as for foldLeft the initial value/result of previous was the first parameter in the function call for foldRight it is the second parameter and the value from the list is the first. So inorder to get my expected result I would need to swap the a and b parameters around.
sentence.foldRight("start")((b,a) => a + b)
Why not see if you can solve problems four and five here using foldLeft and if you are still in need of inspiration checkout the list of foldLeft examples Matt Malones has put together here.







