Press "Enter" to skip to content

foldLeft and foldRight in Scala

Sarfaraz Hussain explains the difference between foldLeft and foldRight in Scala:

The fold method is a Higher Order Function in Scala and it has two variant namely,
i. foldLeft
ii. foldRight
In this blog, we will look into them in detail and try to understand how they work.

Before moving ahead, I want to clarify that the fold method is just a wrapper to foldLeft, i.e. the fold method internally invokes the foldLeft method. So, now let’s get started.

Folding is an extremely powerful technique for getting rid of loops in code. Being comfortable with folding is (in my eyes) one of the signs which indicate that you’ve reached a mid-level understanding of functional programming.