如何在scala咖喱函数中绑定第二个参数?

时间:2016-01-25 11:24:29

标签: scala curry

//a curry function
def find(a: Seq[Int])(sort: (Int, Int) => Boolean)

//My attempt
val findWithBiggerSort = find(_)((a,b) => a > b)

findWithBiggerSort无法正常工作,发生编译错误:

scala> def find(a: Seq[Int])(sort: (Int, Int) => Boolean)
     | ={}
find: (a: Seq[Int])(sort: (Int, Int) => Boolean)Unit

scala> val findWithBiggerSort = find(_)((a,b) => a > b)
<console>:11: error: missing parameter type for expanded function ((x$1) => find(x$1)(((a, b) => a.$greater(b))))
       val findWithBiggerSort = find(_)((a,b) => a > b)
                                     ^

如何绑定第二个咖喱参数?
如何将第二个参数绑定为此

def find(a: Seq[Int], b: String)(sort: (Int, Int) => Boolean)

1 个答案:

答案 0 :(得分:2)

您有两个问题:

sort功能的类型错误 - 您需要(Int, Int) => Boolean,但提供(Int, Int) => Int。如果您将其更改为:

val findWithBiggerSort = find(_)((a,b) => (a > b))

您在_中提供的find (_)参数缺失类型时收到错误消息。如果你提供这种类型,它就会编译,即

val findWithBiggerSort = find(_:Seq[Int])((a,b) => (a > b))

val findWithBiggerSort = find(_:Seq[Int])(_ > _)