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

Using functions in FP ways

There are several common coding patterns that actually take advantage of FP style, even if you weren't aware of it. Let's then get to examine them, and point out the functional aspects of the code, so you can get more accustomed to this coding style.

Injection - sorting it out

A first example of passing functions as parameters is provided by the Array.prototype.sort() method. If you have an array of strings, and you want to sort it, you can just use something like the following code. For example, to alphabetically sort an array with the colors of the rainbow:

var colors = [
// ["blue", "green", "indigo", "orange", "red", "violet", "yellow"]

Note that we didn't have to provide any parameters to the .sort() call, but the array got sorted perfectly well. By default, this method sorts strings according to their ASCII internal representation...