scala> val func: String=> Int = x => Integer.parseInt(x)
func: (String) => Int =
For the beginner, the above snippet might seem quite hard to digest and he might decry - "Scala is hard and not for me". Same was the case when java came to the scene. People were familiar with c style of programming - not familiar with objects. Objects? What are they? Yet OOP is so ubiquitous now. A little persistence is all that is needed. Once you get a hang of it - it becomes a breeze. In fact, when I had started with scala, I had to stare hard to make sense of it. Well, enough of sidetrack.
What we are doing above is - defining function 'from String to Int'(String=> Int) and assigning it to a val called 'func'. x(it could be any other identifier) represents the String input(After all, the function is from String to int) and => Integer.parseInt(x)
represents the body of the function. Cool right?
res1: Int = 200
found : Int(200)
Also, the scala compiler compiles the definition into a class and func is an instance of that class.
res3: Int = 20868995
res5: java.lang.String =
scala> val funcRef = func
funcRef: (String) => Int =
res6: Int = 20868995
So, scala functions are objects after all. Let's extend the above to take in two input parameters instead of one.
scala> val func: (String,String) => Int = (x,y) => Integer.parseInt(x)*Integer.parseInt(y)
func: (String, String) => Int =
res17: Int = 100
res18: java.lang.String =
The above '<
scala> val x=func("10",_: String)
x: (String) => Int =
res19: Int = 100
Here x is a function object which now expects only one parameter.