What Is Functional Programming?

At its core, functional programming is about immutability and about composing functions rather than objects. Many related characteristics fall out of this style.

Functional programs do the following:

Have first-class functions:

First-class functions are functions that can be passed around, dynamically created, stored in data structures, and treated like any other first-class object in the language.

Favor pure functions:

Pure functions are functions that have no side effects. A side effect is an action that the function does that modifies state outside the function.

Compose functions:

Functional programming favors building programs from the bottom up by composing functions together.

Use expressions:

Functional programming favors expressions over statements. Expressions yield values. Statements do not and exist only to control the flow of a program.

Use Immutability:

Since functional programming favors pure functions, which can’t mutate data, it also makes heavy use of immutable data. Instead of modifying an existing data structure, a new one is efficiently created.

Transform, rather than mutate, data:

Functional programming uses functions to transform immutable data. One data structure is put into the function, and a new immutable data structure comes out. This is in explicit contrast with the popular object-oriented model, which views objects as little packets of mutable state and behavior.

A focus on immutable data leads to programs that are written in a more declarative style, since we can’t modify a data structure piece by piece. Here’s an iterative way to filter the odd numbers out of a list, written in Java. Notice how it relies on mutation to add odd numbers to filteredList one at a time.

JavaExamples/src/main/java/com/mblinn/mbfpp/intro/FilterOdds.java
 
public​ ​List​<​Integer​> filterOdds(​List​<​Integer​> list) {
 
List​<​Integer​> filteredList = ​new​ ​ArrayList​<​Integer​>();
 
for​ (​Integer​ current : list) {
 
if​ (isOdd(current)) {
 
filteredList.add(current);
 
}
 
}
 
return​ filteredList;
 
}
 
private​ ​boolean​ isOdd(​Integer​ integer) {
 
return​ 0 != integer % 2;
 
}

And here’s a functional version, written in Clojure.

 
(​filter​ ​odd?​ list-of-ints)

The functional version is obviously much shorter than the object-oriented version. As mentioned previously, this is because functional programming is declarative. That is, it specifies what should be done rather than how to do it. For many problems we encounter in programming, this style lets us work at a higher level of abstraction.

However, other problems are hard, if not impossible, to solve using strict functional programming techniques. A compiler is a pure function. If you put a program in, you expect to get the same machine code out every time. If you don’t, it’s probably a compiler bug. Google’s search engine, however, is not a pure function. If we got the same results from a Google search query every time, we’d be stuck with a late 1990s view of the Web, which would be quite tragic.

For this reason, functional programming languages tend to lie on a spectrum of strictness. Some are more functionally pure than others. Of the two languages we’re using in this book, Clojure is purer on the functional spectrum; at least, it is if we avoid its Java interoperability features.

For example, in idiomatic Clojure, we don’t mutate data as we do in Java. Instead, we rely on an efficient set of immutable data structures, a set of reference types, and a software transactional memory system. This allows us to get the benefits of mutability without the dangers. We’ll introduce these techniques in TinyWeb in Clojure.

Scala has more support for mutable data, but immutable data is preferred. For instance, Scala has both mutable and immutable versions of its collections library, but the immutable data structures are imported and used by default.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.197.95