Java Reference
In-Depth Information
A functional program, however, is modeled on mathematics. The idea is that the inputs to the
application are the arguments to a function, and the outputs of the application are the value that the
function returns. Whereas the object-oriented subordinates may contain their own state, mathematical
functions are stateless: they will always return the same value and have no side effects. A mathematical
function is not so much a behavior as a statement of eternal truth: "f(2)" does not mean “apply 2 to f ”, but
rather “the value when f 's argument is bound to 2”. In functional programming, the goal is to have all the
functions in the application behave like these mathematical functions. This function may be defined in
terms of other functions, but the program is ultimately a single function application, not a series of executive
commands.
f(x,y) = g(x,y) + h(y)
g(x,y) = k(x) + m(y)
h(y) = y
k(x)
m(y)
y
Although functional programming and object-oriented programming are theoretically equivalent, some
solutions are more naturally expressed in one paradigm versus the other: for a given problem, the intuitive
solution within each paradigm can be quite different. In the example I gave at the start of this chapter, object
oriented required us to create a “Predicate” object with the behavior of returning true if the argument is not
null. In functional programming, we simply provide a function that returns true if the argument is not null:
there's no need for an object to hold onto that behavior. In this case, functional programming was the more
intuitive solution to the problem because there was no state, nothing to encapsulate, and inheritance was
nonsensical.
Throughout the early 21st century, many forward-looking developers saw the value in the functional
programming style. That style melds especially well with many development techniques that were then
becoming popularized, such as test-driven development, and it was especially useful in highly distributed
or highly concurrent environments. Many of the problems people were encountering—such as how to send
not just data but also commands over a line—have natural solutions within functional programming style.
Some technology pundits, including your humble author, predicted that a new functional programming
language would arise that would render Java obsolete in much the way that Java has rendered C++ obsolete.
Obviously, this did not happen on the Java Virtual Machine. 2
What happened was actually stranger than we expected. Instead of a pure functional programming
language arising, new object-oriented languages populated the development environment. These languages,
although undoubtedly object oriented, also integrated techniques and capabilities from the functional
programming styles. In these hybrid languages, the developer can work with functions like in the functional
languages, but those functions can have state, making them more like objects. Ruby, Groovy, Scala, and
JavaScript are all examples of this kind of language. After many years, Java has now joined the ranks
with Java 8. Starting with Java 8, Java integrates some of these popular techniques into the core Java
language itself.
2 It has happened on Apple's platform, where Swift is replacing Objective-C. It's also worth noting that Clojure has made
a valiant effort on the JVM front. Some of my peers will defend themselves by pointing to the relative success of Scala,
but Scala is not actually a functional programming language: http://blog.enfranchisedmind.com/2009/05/
scala-not-functional/ .
 
Search WWH ::




Custom Search