Scala:我在写柯里化函数吗?

Scala: am I writing a curried function?

我必须将以下函数转换为柯里化函数:

def findAllUsed(t: List[Task]): List[String] = {
    t.flatMap(x => x.resources.map(x => x.FIELD_TO_SEARCH)).distinct
}

所以我这样做了:

def findAllUsed(t: List[Task], f: Resource => String): List[String] = {
    t.flatMap(x => x.resources.map(f)).distinct
}
findAllUsed(taskSchedules, ((x: Resource) => { x.id }))
findAllUsed(taskSchedules, ((x: Resource) => { x.kind }))

问题是,在我看来,我混淆了柯里化和高阶函数。

任何人都可以,如果我做对了,如果不对,我怎么能做对?

我假设这个练习的意思是这样的:

// separate into two argument lists
def findAllUsed(t: List[Task])(f: Resource => String): List[String] = {
  t.flatMap(x => x.resources.map(f)).distinct
}

// apply the first argument list once, 
// getting a curried function as a result
val curried = findAllUsed(taskSchedules)

// now you can use it twice with different inputs for the second argument list:
curried(x => x.id)
curried(x => x.kind)

这里的好处(如果有的话)是消除了将 taskSchedules 传递给你的函数的重复:因为它有 "it's own" 参数列表,你可以传递它一次,分配结果变成一个值(curried),然后一遍又一遍地重复使用它;

p.s。 curried 的类型是 (Resource => String) => List[String] - 它是从 Resource => String (这是另一个函数...)到字符串列表的函数。