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
}

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)?

1 Answer 1

8

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Simply brilliant!! Do the two have any advantages and disadvantages. The first method looks much cleaner.
@scout: I can't think of any (dis)advantages for either. I made a mistake btw. In the first example it needs an underscore after foo(name).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.