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

Questions


2.1. No extra variables: Our functional implementation required using an extra variable, done, to mark whether the function had already been called. Not that it matters... but could you make do without using any extra variables? Note that we aren't telling you not to use any variables; it's just a matter of not adding any new ones, such as done, and only as an exercise!

2.2. Alternating functions: In the spirit of our onceAndAfter() function, could you write an alternator() higher-order function that gets two functions as arguments, and on each call, alternatively calls one and another? The expected behavior should be as in the following example:

     let sayA = () => console.log("A");
     let sayB = () => console.log("B");

     let alt = alternator(sayA, sayB);
     alt(); // A
     alt(); // B
     alt(); // A
     alt(); // B
     alt(); // A
     alt(); // B

2.3. Everything has a limit!: As an extension of once(), could you write a higher-order function thisManyTimes...