Listing 1, Listing 2, and Listing 3 show an
example of code simplification through
the use of lambda expressions.
In Listing 1, existing collections impose external iteration. The client of the
collection determines how to iterate.
The implementation of accumulation
is overspecified, and computation is
achieved through side effects (
assignment to highestScore).
Listing 2 uses internal iteration, so the
iteration and accumulation are embodied in the library. For example, filtering
can be done in parallel, and the client is
more flexible, more abstract, and less
error prone.
Listing 3 uses lambda expressions,
which represent “code as data.” The
lambda expression is introduced with
special syntax using zero or more-formal
parameters. Parameter types are optional and may be inferred, and the body
may be an expression or statements. If
the body is an expression, there is no
need for return or ;.
We refer to internal iteration when
the collection is responsible for iterating
or in some way processing its data. By passing the
programming logic to the
collection and letting the
collection iterate through
its elements, the program is
potentially much more
efficient and robust than
with external iteration,
where the iteration logic
lives in the user’s program.
Having lambda expres-
sions in the language
makes it much more natural to design
and use “higher-order functions” in
which the programmer specifies just
the atomic operations to be performed
on the data, and the collection figures
out how to accomplish the task. Using
this technique, many applications can
be written in a way that is more compact
and reliable. In a sense, lambda expres-
sions are the fundamental primitive
in a programming language, and
now they can be implemented in an
efficient manner.
double highestScore = 0.0;
for (Student s : students) {
if ( s.grad Year == 2011) {
if ( s.score > highestScore) {
highestScore = s.score; }
}
}
COMMUNITY
JAVA IN ACTION
See all listings as text
implement in terms of specification.
It’s a much more structured process
than most open source projects, though
I think the worlds of open source and
community standards are moving
closer together.
It’s important to have a deeply involved Expert Group like John Rose had
for JSR-292. Expert Group members
contribute their deep experience. For
instance, a language implementer for
JRuby might say, “My life would be easier
if the JVM helped me do this . . . .” As a
JSR progresses, external contributors
also provide reports and the benefit of
“in the trenches” observations.
I think JSR-292 did a remarkable job
coming up with a design that satisfies
language implementers and is fully im-
plementable with proper performance
by all the major JVM vendors. The design
that was committed to will be part of
the Java SE platform “forever,” because
while JVM implementations come and
go, the JVM itself is a lasting entity. There
will probably be class files in computing
environments 50 years from now! At this
level of the platform, everything must be
impeccably stable and reliable.
ABOUT US
In a sense, lambda
expressions are the
fundamental primitive
in a programming
language, and now they
can be implemented in
an efficient manner.
Michael Meloan began his professional career
writing IBM mainframe and DEC PDP- 11
assembly languages. He went on to code in
PL/I, APL, C, and Java. In addition, his fiction has appeared in WIRED, BUZZ, Chic, LA
Weekly, and on National Public Radio. He is also
a Huffington Post blogger.
LEARN MORE
•;JSR-292: “Supporting Dynamically Typed
Languages on the Java Platform”
•;Project Lambda
•;Java SE
blog