I am not sure what is the difference between fold
and foldLeft
in Scala.
The question Difference between fold and foldLeft or foldRight? has an
NOTE: I could be completely wrong here. My scala is less than perfect.
I think that the difference is in the signature of the methods:
def fold[A1 >: A](z: A1)(op: (A1, A1) ⇒ A1): A1
vs
def foldLeft[B](z: B)(op: (B, T) ⇒ B): B
In short, fold is defined as operating on some type A1 which is a supertype of the array's type, which for your string array the compiler defines as "Any" (probably because it needs a type that can store your String or an int- notice that the combiner method passed to fold Fold takes two parameters of the same type?) That's also what the documentation means when it talks about z- the implementation of Fold could be such that it combines your inputs in parallel, for instance:
"1" + "2" --\
--> 3 + 3 -> 6
"3" + *z* --/
On the other hand, foldLeft operates on type B (unconstrained) and only asks that you provide a combiner method that takes a parameter of type B and another of your array's type (String, in your case), and produces a B.