Scala, Haskell, and F# [2], among others. Before Java 8, Java
programmers have been forced into writing more verbose code
than needed and lacked key functionality such as the ability to
pass in a function as a parameter.
The following subsections give an overview of lambda
expressions. This discussion includes what a lambda
expression is, why they are useful, and finally their
performance in other languages.
2.1 What is a Lambda Expression?
Lambda expressions are also known as anonymous
functions because they are functions without an identifier.
These expressions can make use of already programmed
functional interfaces, such as a Predicate or Function. With no
identifier, a lambda expression isn’t intended to be called
many times like a method. They are actually commonly used
to avoid coding unnecessary methods. Thus, if the
functionality is only needed once or for a short amount of
time, lambda expressions help make code clearer and concise
[3].
Example:
/*to get the total + tax of a list of prices */
ArrayList<Integer> prices = new
ArrayList<Integer>(Arrays.asList(90,87,34,21));
// using lambda expression
double total = prices.stream()
.mapToDouble(x -> x*1.14)
.sum();
//using non-lambda expression
double total2 = 0.0;
for (Integer i : prices) {
total2 += i*1.14;
}
For example, the above lambda expression can be written
using one line of code. The non-lambda expression involves
first initializing a variable and then creating a for-loop to add
each item price, with tax, to the total.
2.2 Why Use Lambda Expressions?
As stated in [3], the use of functional interfaces paired
with anonymous inner classes is a common theme in Java. To
simplify the coding, functional interfaces are taken advantage
of for use with lambda expressions, eliminating the need to
program inner classes.
Example:
// Using inner class
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
// Using lambda expression
btn.setOnAction(event->
System.out.println("Hello World!"));
The “horizontal solution” of using lambda expressions,
solves the “vertical problem” presented by using inner classes
[3]. A lambda expression addresses the bulkiness of an inner
class by converting 5, 6, or even more lines of code into a
single statement.
2.3 Performance in Other Programming
Languages
Performance of lambda expressions differs from one
programming language to another. In some languages, the use
of a lambda expression not only gives the code a more clear
and concise look, but also has a faster execution time.
However, in other programming languages, code runs faster
using the more verbose approach [6]. As stated in [6], the
“lazy” approach can have its costs when it comes to
efficiency; using lambda expressions when needed is slower
than calling a function by name. The following subsections
review the use of lambda expressions in other popular
programming languages.
2.3.1 In Haskell
Haskell is a purely functional programming language,
based on lambda calculus. In the release of Haskell version
1.0, in 1990, it was well known that the use of lambda
expressions caused a significant and constant performance loss
[6]. Haskell 1.0 was also inefficient when it came to defining
streams [6], making it more inefficient to use lambda
expressions on streams of data.
Haskell Prime was released in 2006, where much of the
development focus was on performance [6]. Now competitive
performance is available with Haskell [6]. Haskell
programmers can now use the functionality of lambda
expressions without the inferiority of performance.
2.3.2 In Python
According to Python’s official documentation, lambda
expressions are equivalent to regular function objects [8]. In
Python, lambda expressions are just a better syntactic way to
write a normal function [9]. Thus, the implication is that
lambda expressions have equivalent performance as non-
lambda expressions in Python. However, as stated in [10],
lambda expressions in Python can be more efficient to use for
common programming idioms such as mapping, filtering, and
list comprehension.
2.3.3 In C++
C++11 was released in 2011 and saw major revisions
including the use of lambda expressions [11]. According to the
ISO (International Organization for Standardization), the
addition of lambda expressions to C++ has added much
strength flexibility and efficiency [11]. C++ programmers are
Int'l Conf. Software Eng. Research and Practice | SERP'15 |