Functional Programming: What It's All About
Functional Programming emphasizes the evaluation of expressions rather than the execution of commands. Tracing back to calculus mathematics, the main goal of Functional Programming is to prevent the inadvertent changing of already calculated values. As developers know, the most likely cause of accidental variable modifications are functions that have access to global objects. The reason accessing of global variables by multiple functions is inherently dangerous is that changing a global variable in part of a program can have unexpected effects in another part. Functional programming isn't a replacement for Object-Oriented and Pricedural/Imperative styles; each has a place in Web development. Examples of Functional Programming languages include Scheme, Erlang and Haskell.
Higher Order Functions
- You can pass functions to other functions.
- Functions can return another function.
- Functions have scope, just like any other variable.
Have a look at the following code and see how functions can be utilized in Higher Order programming:
In the above example, the
testFor() function accepts another function as an
argument and applies it to each of the passed function's arguments until it
true. If none of the function calls return
true, the function exits
without returning anything. This will be evaluated as
undefined by any subsequent
tests and interpreted as
false. Another trait of Higher Order functions is the
returning of another function. This is exactly what happens here, since the
function doesn't return the results of the passed function, but another
function which contains the passed function. The returned function is
stored in the
testForEvenNumbers variable and subsequently called like
any other function.
We can further incorporate Functional Programming into our
indexOf() Array methods. The
map() method takes a function as a parameter and applies it to each element in
an array. The
indexOf() function searches through an array and returns the index
of the first element that contains the object we're looking for. A search
that fails to turn up the object in question returns an index of
-1. It accepts
the object we're seeking, as well as an optional element index. The only problem is the function's
arguments property isn't
an array. Before calling
indexOf(), we need to convert the
into a proper
Array object. This is done by applying the
method. From there, we can chain the functions together by mapping the
passed function (
fn) to all the elements in the
arguments array and then calling
indexOf() against the returned values, searching for the
true boolean value:
This is a powerful way of writing code because it leverages library functions that are well documented and tested, thus requiring less writing and testing on our part.