foreach is not for returning results, use a map instead:
val result = Range (0, max).map (generatorFunctionReturningInt).map (sum _)
in particular, the amount is already predetermined, right?
val result = Range (0, max).map (generatorFunctionReturningInt).sum
working code (we all like working code)
val result = Range (0, 15).map (3 + _).sum result: Int = 150
For this trivial case, this is the same as Range (3, 18).sum .
source share