I have the following function
scala> def f1 = (prefix: String) => prefix + ".field"
f1: String => java.lang.String
And I’d like to define another function from f1, that fixed the value of prefix to p1, like this
def f2: () => String = () => f1("p1")
or more shortly
def f2 = () => f1("p1")
I think that the same could be achieved using Function.curried or f.curried and partialy applied functions, but I still couldn’t do it…
—
Having a look a this article I found a more verbose way of defining it. I guess the above syntax is just suger for this longer form…
scala> object f2 extends Function0[String] {
| override def apply = f1("p1")
| }
defined module f2
scala> f2
res37: f2.type = <function0>
scala> f2()
res38: java.lang.String = p1.field
You can only “curry” functions with more than one argument. At least with the methods that Scala provides by default. With two parameters it works like this:
If you want to do the same with
Function1, you can “pimp” the class to add a new method: