Scala/Function literals  From Wikibooks, open books for an open
world Jump to: navigation, search  It often occurs that a
function is defined only to be used once. Since it is only
defined once, it is somewhat superfluous to give it a name. To
avoid repeatedly naming and using functions once, there exists a
facility to just avoid naming them at all. These are called
anonymous functions or function literals. A simple example of a
function turned into an anonymous version:  def
isEqualToFour(a:Int) = a == 4 (a:Int) => a == 4  The two
functions above are effectively the same, except that the second
one does not have a name. But the above example is not very
exciting. So let's see it in action by applying using it in a
higher-order function:  list.exists((a:Int) => a == 4)  Quite a
bit shorter, since we previosly had to write "isEqualToFour"
twice, and use a "def". But it can be shorter. Since the
function literal is used inside a function, the Scala compiler
can infer the argument types:  list.exists(a => a == 4