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)
Dedication
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
8
Connecting Functions - Pipelining and Composition
Bibliography
Answers to Questions

The straightforward JS way


One of the biggest causes of side-effects was the possibility of a function modifying either global objects or its arguments themselves. All non-primitive objects are passed as references, so when/if you modify them, the original objects will be changed. If we want to stop this (without just depending on the goodwill and clean coding of our developers) we may want to consider some straightforward JS techniques to disallow those side-effects.

Mutator functions

A common source of unexpected problems comes from the fact that several JS methods actually modify the underlying object. In this case, by merely using them, you will be causing a side-effect, which you may even not recognize. Arrays are the basic source of problems and the list of troublesome methods is not short. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Mutator_methods for more on each method.)

  • .copyWithin() lets you copy elements within the array
  • .fill() fills...