1

I am new to Scala and trying to explore more in the functional approach. I have written a method and define a variable like this:-

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)

Now, my Question is that, is both oddHOF and getFilterList are higher order function if not then what oddHOF and getFilterList be called ?

0__
  • 66,707
  • 21
  • 171
  • 266
Akash Sethi
  • 2,284
  • 1
  • 20
  • 40

1 Answers1

1

A Higher ordered function is a function that takes function as parameter. Hence, getFilterList is a higher ordered function since it takes function of type Int => Boolean as parameter.

On the other hand, oddHOF is First 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.

Ra Ka
  • 2,995
  • 3
  • 23
  • 31
  • True except that `getFilterList` is a method, not a function (but perhaps that's a bit of needless hair splitting). – jwvh Aug 14 '17 at 04:42
  • @jwvh what exactly is difference between function and method ? – Akash Sethi Aug 14 '17 at 04:50
  • 1
    @AkashSethi, A topic of much confusion. Discussed at length [here](https://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala) and [here](https://stackoverflow.com/questions/4839537/functions-vs-methods-in-scala). – jwvh Aug 14 '17 at 04:56