Surender Raja Surender Raja - 1 year ago 118
Scala Question

In scala map, filter,grouby are functions or methods

I am new to scala. I need to understand one basic idea.

In scala, we have map,filter,groupBy etc..

Are they functions? or Are they methods?

Answer Source

They are methods. See, e.g. the method.

There aren't really functions in the Scala Core Language.

Basically, the support for functions in Scala is similar to Java: any object which has a method called apply, can be used as a "function". There are a number of traits in the Scala Standard Library, called Function0[+R], Function1[-T1, +R], Function2[-T1, -T2, +R], Function3[-T1, -T2, -T3, +R], etc. which can be instantiated using special literal syntax like this:

val fn = (i: Int, j: Int) => i + j

There are two ideas what it means to be a "function" in Scala:

  • the narrow one: an instance of one of the FunctionN traits, or
  • the general one: any object which has an apply method.

map, filter, groupBy, are neither, they aren't even objects. They are methods.