Wednesday, December 25, 2013

Understanding Function literal and Function value

Function Literal and Function Value

This is one of the nice ingredients of Scala that allows developers to write an unnamed (or anonymous) function and assign it to a variable. This unnamed function is called as a Function Literal. Look at the below example,

val vowels = List ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', U)
val hasVowelChar = (s: String) =>  s.toCharArray.exists(vowels.contains(_))

The variable hasVowelChar have been assigned an unnamed function with string parameter and returns true if string contains vowel character.

The above said unnamed function gets instantiated at run time when this function is invoked which is called Function Value (hasVowelChar).  

No comments:

Post a Comment