Книга: Functional Programming in JavaScript
Назад: What makes a language functional?
Дальше: Functional programming in a nonfunctional world

Advantages

You could say that the profound enlightenment you experience when you finally "get it" will make learning functional programming worth it. An experience such as this will make you a better programmer for the rest of your life, whether you actually become a full-time functional programmer or not.

But we're not talking about learning to meditate; we're talking about learning an extremely useful tool that will make you a better programmer.

Formally speaking, what exactly are the practical advantages of using functional programming?

Cleaner code

Functional programs are cleaner, simpler, and smaller. This simplifies debugging, testing, and maintenance.

For example, let's say we need a function that converts a two-dimensional array into a one-dimensional array. Using only imperative techniques, we could write it the following way:

function merge2dArrayIntoOne(arrays) {   var count = arrays.length;   var merged = new Array(count);    var c = 0;   for (var i = 0; i < count; ++i) {     for (var j = 0, jlen = arrays[i].length; j < jlen; ++j) {       merged[c++] = arrays[i][j];     }   }   return merged }

And using functional techniques, it could be written as follows:

varmerge2dArrayIntoOne2 = function(arrays) {   return arrays.reduce( function(p,n){     return p.concat(n);   }); };

Both of these functions take the same input and return the same output. However, the functional example is much more concise and clean.

Modularity

Functional programming forces large problems to be broken down into smaller instances of the same problem to be solved. This means that the code is more modular. Programs that are modular are clearly specified, easier to debug, and simpler to maintain. Testing is easier because each piece of modular code can potentially be checked for correctness.

Reusability

Functional programs share a variety of common helper functions, due to the modularity of functional programming. You'll find that many of these functions can be reused for a variety of different applications.

Many of the most common functions will be covered later in this chapter. However, as you work as a functional programmer, you will inevitably compile your own library of little functions that can be used over and over again. For example, a well-designed function that searches through the lines of a configuration file could also be used to search through a hash table.

Reduced coupling

Coupling is the amount of dependency between modules in a program. Because the functional programmer works to write first-class, higher-order, pure functions that are completely independent of each other with no side effects on global variables, coupling is greatly reduced. Certainly, functions will unavoidably rely on each other. But modifying one function will not change another, so long as the one-to-one mapping of inputs to outputs remains correct.

Mathematically correct

This last one is on a more theoretical level. Thanks to its roots in Lambda calculus, functional programs can be mathematically proven to be correct. This is a big advantage for researchers who need to prove the growth rate, time complexity, and mathematical correctness of a program.

Let's look at Fibonacci's sequence. Although it's rarely used for anything other than a proof-of-concept, it illustrates this concept quite well. The standard way of evaluating a Fibonacci sequence is to create a recursive function that expresses with a base case to , which makes it possible to stop the recursion and begin adding up the values returned at each step in the recursive call stack.

This describes the intermediary steps involved in calculating the sequence.

var fibonacci = function(n) {   if (n < 2) {     return 1;   }   else {     return fibonacci(n - 2) + fibonacci(n - 1);   } } console.log( fibonacci(8) ); // Output: 34

However, with the help of a library that implements a lazy execution strategy, an indefinite sequence can be generated that states the mathematical equation that defines the entire sequence of numbers. Only as many numbers as needed will be computed.

var fibonacci2 = Lazy.generate(function() {   var x = 1,   y = 1;   return function() {     var prev = x;     x = y;     y += prev;     return prev;   }; }());  console.log(fibonacci2.length());// Output: undefined  console.log(fibonacci2.take(12).toArray());// Output: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]   var fibonacci3 = Lazy.generate(function() {   var x = 1,   y = 1;   return function() {     var prev = x;     x = y;     y += prev;     return prev;   }; }());  console.log(fibonacci3.take(9).reverse().first(1).toArray());// Output: [34]

The second example is clearly more mathematically sound. It relies on the library of JavaScript. There are other libraries that can help here as well, such as and . These will be covered in , Setting Up the Functional Programming Environment.

Назад: What makes a language functional?
Дальше: Functional programming in a nonfunctional world

bsn
thank
Vesa Karvonen
I hope you don't mind, but I’d like to point you and your readers to my high-performance optics library for JavaScript that is in production use in multiple projects, has comprehensive support for partial optics and interactive documentation: https://calmm-js.github.io/partial.lenses/ (this takes a moment to load — be patient!)