开发者

Partially applied recursive functions

开发者 https://www.devze.com 2023-01-06 07:01 出处:网络
def mainCaller() = { val name = \"xyz\" someList.foreach { u:Map => foo(name, u) } } def foo(name:String)(map:Map): Unit = {
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
}

how can I write foo as a partially applied function, where I开发者_如何学JAVA dont have to pass name in every recursive call and just call foo(map1)?


Two options:

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

    // case 2:
    bar(y)
}

Or:

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

        // case 2:
        bar(y)
    }
    bar
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号