A Notes about Scala Anonymous Functions
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:
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:
It turns out that
filter method took an argument of boolean function. So, this part should be a boolean function then:
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:
This post from Scala Doc provides good resources about Scala anonymous function.