Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially applied recursive functions

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 dont have to pass name in every recursive call and just call foo(map1)?

like image 400
scout Avatar asked Jun 29 '26 10:06

scout


1 Answers

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
}
like image 191
sepp2k Avatar answered Jul 01 '26 23:07

sepp2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!