I'm trying to figure out the rhyme or reason to where parenthesis are used in Scala, in particular with the fold method:
val sum = prices.fold(0.0)(_ + _)
Why is it like that? Scala doesn't seem to follow the FP format in general (no parenthesis on method calls), but there it does... except it also requires parenthesis among the single-term accumulator? Why isn't either
val sum = prices.fold((0.0), (_ + _))
or
val sum = prices.fold 0.0 (_ + _)
?
IterableOnce.fold
is defined using multiple parameter lists perhaps due to type inference reasons as suggested by Jörg W Mittag
def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1
on the other hand, for example, Either.fold
is defined using a single parameter list
def fold[C](fa: A => C, fb: B => C): C
Confirming type inference reasons, consider the following example
def zar[T](x: T)(f: (T, T) => T) = f(x, x)
def tar[T](x: T, f: (T, T) => T) = f(x, x)
zar(21)(_ + _) // OK
tar(21, _ + _) // Error: missing parameter type for expanded function
tar[Int](21, _ + _) // OK
Note how we had to explicitly specify type parameter to tar
when not using multiple parameter lists.