具有“Val”和“Def”的高阶函数

Higher Order Function with `Val` and `Def`

我是 Scala 的新手,正在尝试探索更多功能方法。 我写了一个方法并定义了一个这样的变量:-

val list = 1 to 10 toList

def getFilterList(list: List[Int],f:Int => Boolean): List[Int] = {
  list.filter(f)
}

getFilterList(list, x => x %2 ==0)

val oddHOF :Int => Boolean = value => value % 2 == 0

list.filter(oddHOF)

现在,我的问题是,oddHOFgetFilterList 都是高阶函数吗?如果不是,那么 oddHOFgetFilterList 应该叫什么?

A Higher ordered function是一个以函数为参数的函数。因此,getFilterList 是一个高阶函数,因为它采用 Int => Boolean 类型的函数作为参数。

另一方面,oddHOFFirst class function, which means you can express functions in function literal syntax. e.g. val oddHOF: Int => Boolean = (value:Int) => value % 2 == 0. Here, the type of function is Int => Boolean, i.e. it takes one parameter of type Int and return boolean value and (value:Int) => value % 2 == 0 is a function literal