3
def mainCaller() = { 
  val name = "xyz"
  someList.foreach { u:Map => foo(name, u) }
}

def foo(name:String)(map:Map): Unit = {
  //match case....
  //recursive call to foo in each case where name remains same, but map changes
}

如何将 foo 编写为部分应用的函数,我不必在每个递归调用中传递名称而只需调用foo(map1)

4

1 回答 1

8

两种选择:

def foo(name:String)(map:Map): Unit = {
    val bar = foo(name)_
    //match case...
    // case 1:
    bar(x)

    // case 2:
    bar(y)
}

或者:

def foo(name:String): Map => Unit = {
    def bar(map: Map): Unit = {
        //match case...
        // case 1:
        bar(x)

        // case 2:
        bar(y)
    }
    bar
}
于 2010-07-06T00:21:54.013 回答