2016-05-06
*post*
================================================================================

Anonymous Function in Scala

================================================================================

Scala is my first functional programming language. Recently I wrote a lot of codes in Scala to build a Data processing/analytics and Machine Learning application using Apache Spark.

Looking at source code like this:

val lineWithSaleStock = textFile.filter(line => line.contains("Sale Stock"))

My first impression was: “Where the hell is line coming from?”. At previous line, there is no definition of line variable/value at all.

I decided to look at Spark API and found the following filter method definition:

def filter(f: (T) ⇒ Boolean): RDD[T]
    Return a new RDD containing only the elements that satisfy a predicate.

It turns out that filter method took an argument of boolean function. So, this part should be a boolean function then:

line => line.contains("Sale Stock")

Ahh interesting, line is the input parameters and it returns a boolean value from line.contains("Sale Stock") one. And that’s part is a Scala Anonymous Function.

It seems, Scala is have a nice syntax to express the anonymous function. I can express previous function as:

val isContainsSaleStock = (s: String) => s.contains("Sale Stock")
val lineWithSaleStock = textFile.filter(isContainSaleStock)

This post from Scala Doc provides good resources about Scala anonymous function.

================================================================================

TAGS

*post-tags*

================================================================================

LINKS

*post-links*