How to determine a function that accepts a currency parameter of a function?

Below fn2failed to compile,

def fn(x: Int)(y: Int) = x + y
def fn2(f: ((Int)(Int)) => Int) = f
fn2(fn)(1)(2) // expected = 3

How to determine fn2for adoption fn?

+4
source share
1 answer

It should be as follows:

scala> def fn2(f: Int => Int => Int) = f
fn2: (f: Int => (Int => Int))Int => (Int => Int)

scala> fn2(fn)(1)(2)
res5: Int = 3

(Int)(Int) => Intfalse - should be used instead Int => Int => Int(e.g. in Haskell). In fact, the curried function accepts Intand returns a Int => Intfunction.

PS You can also use fn2(fn _)(1)(2), since the transfer fnin the previous example is just a short form of eta extension, see Differences between using underscores in these scala methods .

+11
source

All Articles