λ Tony's Blog λ
scala.List.foldLeft for Java programmersPosted on January 15, 2008
I recently ran another Scala training course and I produced a hand-out that seemed to help people understand what a foldLeft is. Here is a brief explanation of it.
Suppose you have the following Java code:
You can have any value for
method may ignore any of its arguments or even not have them passed at all. You’ll notice that a lot of Java code is written this way, so it would be fair to say that “left folds occur a lot in Java”, despite being encoded as loops.
The above Java code is equivalent to the following Scala code, again for all values of the given identifiers (so long as they type check of course):
…which is also equivalent to:
(start /: listOfA)(method)
…which by the way, is a perfectly sound approach to performing a list reduction, despite what some clowns rubbish on with.
That’s all, hope it helped :)