Book Image

Mastering JavaScript Functional Programming

By : Federico Kereki
Book Image

Mastering JavaScript Functional Programming

By: Federico Kereki

Overview of this book

Functional programming is a programming paradigm for developing software using functions. Learning to use functional programming is a good way to write more concise code, with greater concurrency and performance. The JavaScript language is particularly suited to functional programming. This book provides comprehensive coverage of the major topics in functional programming with JavaScript to produce shorter, clearer, and testable programs. You’ll delve into functional programming; including writing and testing pure functions, reducing side-effects, and other features to make your applications functional in nature. Specifically, we’ll explore techniques to simplify coding, apply recursion for loopless coding, learn ways to achieve immutability, implement design patterns, and work with data types. By the end of this book, you’ll have developed the JavaScript skills you need to program functional applications with confidence.
Table of Contents (22 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback
Connecting Functions - Pipelining and Composition
Answers to Questions

Other higher-order functions

Let's end this chapter by considering other sundry functions that provide results such as new finders, decoupling method from objects, and more.

Turning operations into functions

We have already seen several cases in which we needed to write a function just to add or multiply a pair of numbers. For example, in the Summing an array section of Chapter 5, Programming Declaratively - A Better Style, we had to write code equivalent to the following:

const mySum = myArray.reduce((x, y) => x + y, 0);

In the same chapter, in the section Working with ranges, to calculate a factorial, we then needed this:

const factorialByRange = n => range(1, n + 1).reduce((x, y) => x * y, 1);

It would have been easier if we could just turn a binary operator into a function that calculates the same result. The preceding two examples could have been written more succinctly, shown as follows:

const mySum = myArray.reduce(binaryOp("+"), 0);
const factorialByRange = n => range(1, n...