Search code examples
scalascala-placeholder-syntax

How to pass Function result to math.max


I am able to do this:

  def largestAt(fun: (Int) => Int, inputs: Seq[Int]):Int = {
    inputs.reduceLeft(math.max(_,_))
  }

But, when I am trying to do this:

def largestAt(fun: (Int) => Int, inputs: Seq[Int]):Int = {
   inputs.reduceLeft(math.max(fun(_),fun(_)))
}

I am getting below mentioned compilation error:

overloaded method value max with alternatives:
[error]   (x: Double,y: Double)Double <and>
[error]   (x: Float,y: Float)Float <and>
[error]   (x: Long,y: Long)Long <and>
[error]   (x: Int,y: Int)Int
[error]  cannot be applied to (Int => Int, Int => Int)
[error]     inputs.reduceLeft(math.max(fun(_),fun(_)))

I just want to pass the result of the fun to math.max call. How can I achieve this.

Thanks in advance...


Solution

  • Anonymous function placeholder parameter syntax in the expression

    inputs.reduceLeft(math.max(fun(_),fun(_)))
    

    expands to something like

    inputs.reduceLeft(math.max(a => fun(a), b => fun(b)))
    

    and max does not take a pair of functions as input. Instead, you are likely after something like

    inputs.reduceLeft((a, b) => math.max(fun(a), fun(b)))
    

    On the other hand the following usage of underscore works

    inputs.reduceLeft(math.max(_, _))
    

    because it expands to

    inputs.reduceLeft((a, b) => math.max(a, b))
    

    Placeholder syntax should probably be used judiciously, and the key to using it correctly is understanding the scope of underscore:

    If the underscore is inside an expression delimited by () or {}, the innermost such delimiter that contains the underscore will be used;