Partially applied Function2 with the first and second argument

For method

def f(a: String, b: String) = ???

I want to get both a partially applied function (with the first argument, and with the second).

I wrote the following code:

def fst = f _ curried "a"
def snd = (s: String) => (f _ curried)(s)("b")

Is there a better way?

[update] snd can be written shorter def snd = (f _ curried)((_: String))("b")

+3
source share
2 answers

It is easier:

def fst = f("a", (_: String))
def snd = f((_: String), "b")

But note that it recounts the partially applicable functions for each call, so I would prefer val(or perhaps lazy val) instead of defin most cases.

+5
source

Another way to do this is to use several parameter lists:

def f(a: String)(b: String) = ???

No arguments:

def fst = f _

First argument:

def fst = f("a")

Only the second argument:

def fst = f(_: String)("b")

All arguments:

def snd = f("a")("b")

args , , , , .

+2

All Articles