'missing parameter type for expanded function (Scala)

Here's my function:

def sumOfSquaresOfOdd(in: Seq[Int]): Int = {
  in.filter(_%2==1).map(_*_).reduce(_+_)
}

Why am I getting the error "missing parameter type for expanded function"?



Solution 1:[1]

I guess because map wants a function of a single argument, and you ask it to call * with two arguments. Replace _ * _ with arg => arg * arg and retry.

Solution 2:[2]

"map" was called with a function with two parameters when it is expecting a function of one parameter. There's another small bug due to the use of "reduce" - it throws an exception if there aren't any odd Ints in the input Seq.

A better solution would be:

  def sumOfSquaresOfOdd(in: Seq[Int]): Int =
    in.filter(_ % 2 == 1) . map(x => x * x) . sum

You must be using Scala2. Scala3 gives a much better error message:

Error:
2 |  in.filter(_%2==1).map(_*_).reduce(_+_)
  |                        ^^^
  |                        Wrong number of parameters, expected: 1

(Edited to reflect changes in the other answers to this question.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chrnybo
Solution 2