2

I've been wondering, why there signatures for fold and fold[Left|Right] are different (apart from the name of course.

misza222
  • 393
  • 3
  • 15

1 Answers1

4

Subtle and very important difference

fold can be executed in parallel because the seed element can be passed to an arbitrary number of workers.

In other words the next invocation does not depend on the last invocation

On the other hand foldLeft and foldRight must be executed sequentially because for the B parameter to be available for the second element it must first be computed for the first element of the sequence.

Less important and more obvious difference:

Note the seed argument to fold must match the type of the elements in the collection. foldLeft and foldRight don't have this restriction, they will always return an element with type equal to the type of the seed used.

pedromss
  • 2,443
  • 18
  • 24