I'm a Java software developer, consultant and architect that focuses on enterprise applications, quality and performance. I gained interest in Java due to it's open nature and community support. Next to Java, I spend most of my time trying to stay up to date with everything that moves inside the software world like Scala and NoSQL db's. Jelle is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Understanding foldLeft in Scala

  • submit to reddit

 As I’m working on some scala code the last few weeks, i have come across some functions in the scala api that I didn’t understand right away. One of those functions was foldLeft, FoldLeft is a relative easy method, but the scaladoc is not quite clear.

Applies a binary operator to a start value and all elements of this list, going left to right.

What the method actually does is apply a method on all the elements of a Sequence and accumulating it a variable. The initial value of that variable is give as first parameter, the second parameter is the function you apply.
In Java it would like this

T accumulator = initialValue;
for(X listItem : list){
    accumulator = method(accumulator, listItem);
return accumulator;

Take is an integer and method does “accumulator + listItem”, we get a method that adds all integers with eachother starting from the initial value.

val list = List(1,2,3)
list.foldLeft(0)(_ + _)

The first parameter of the function given as argument is the accumulator, the second is the current value (or listItem in the java example). foldLeft results in 6.

Published at DZone with permission of Jelle Victoor, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Oliver Plohmann replied on Tue, 2012/11/13 - 3:21am

Nice, but Smalltalk-80 had it about 30 years ago already:

| list sum |
list := OrderedCollection new add: 1; add: 2; add: 3; yourself.
sum := list inject: 0 into: [ :a :b | a + b ].
Transcript cr; show: sum.  "prints 6"

The young people couldn't have known this ... ;-)

Attila Magyar replied on Tue, 2012/11/13 - 2:46pm in response to: Oliver Plohmann

and it is simple, uniform and expressive. smalltalk rulez

Konrad Malawski replied on Sat, 2012/11/24 - 7:58pm in response to: Oliver Plohmann

Hey Oliver - just wanted to thank you for sharing the smalltalk snippet. 

While learning many languages this is one I still haven't tried - and time and time again (like now) I'm being reminded I should go sit down and give it a shot - even if just for "historical reasons" :-)

-- Konrad

Oliver Plohmann replied on Fri, 2012/11/30 - 8:03am in response to: Konrad Malawski

 Hi Konrad,

glad you liked the little code snippet. In case you are interested this a page with lots of free Smalltalk books (free because meanwhile out of print). Then there is the free Squeak Smalltalk system made by Dan Ingalls and Alan Kay (the original creators of Smalltalk-80). The Look & Feel is (on purpose) similar to the one of the original Smalltalk-80, which looks nowadays a bit outdated. But that's only the L&F. The IDE itself has lots functionality.

Cheers, Oliver

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.