Difference between fold and reduce (or) reduce vs fold.

Not a common question but lot of developers are confused between these two functions including me. So I thought of writing a blog with some example code snippets to clarify the difference between these two functions.



Reduce operation assumes commutative/associative property true. It combines all the elements that are present in RDD together in parallel like sum.

val list = List(1,2,3,4,5,6)





Fold is same as reduce but it takes the initial value for the result. Commutative property will be assumed to false. The initial value that you pass to this function must be identity element for you operation. If the operation is addition  it must be 0 or if the operation is multiplication, it must be 1.


Here is sample run of both the functions:


Leave a Reply

Your email address will not be published. Required fields are marked *