scala – 如何定义函数接受curried函数参数?
发布时间:2020-12-16 18:22:42 所属栏目:安全 来源:网络整理
导读:fn2下面编译失败, def fn(x: Int)(y: Int) = x + ydef fn2(f: ((Int)(Int)) = Int) = ffn2(fn)(1)(2) // expected = 3 如何定义fn2来接受fn? 解决方法 它应该如下: scala def fn2(f: Int = Int = Int) = ffn2: (f: Int = (Int = Int))Int = (Int = Int)sca
fn2下面编译失败,
def fn(x: Int)(y: Int) = x + y def fn2(f: ((Int)(Int)) => Int) = f fn2(fn)(1)(2) // expected = 3 如何定义fn2来接受fn? 解决方法
它应该如下:
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)=> Int不正确 – 你应该使用Int => Int => Int(就像在Haskell中一样).实际上,curried函数接受Int并返回Int => Int功能. 附:你也可以使用fn2(fn _)(1)(2),因为在前面的例子中传递fn只是eta扩展的一种简短形式,见The differences between underscore usage in these scala’s methods. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |