Search code examples
scalaforeachpartial-application

Scala: curried function in foreach?


I am trying to use curried function when iterating collection with 'foreach' method:

object CurriedTest {

  def main(args: Array[String]): Unit = {
    fun("one")("two")("three")
    (fun2)("three")
    val lst = List ("x", "y", "z")
    lst.foreach(fun2) 
    lst.foreach(fun("one"),("two") _) 

  }

  def fun (a1: String) (a2:String) (a3: String) = {
    println("a1: "+a1+" a2: "+a2+" a3: "+a3)
  }

  def fun2 = fun("one")("two") _
}

Why line 'lst.foreach(fun("one"),("two") _) ' does not compile and return:

- too many arguments for method foreach: (f: String => B)Unit

error message?


Solution

  • Remove comma from this line

    lst.foreach(fun("one"),("two") _)