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

Functions as data structures


So far, we have seen how to use functions to work with or transform, other functions, to process data structures, or to create data types. Let's then finish this chapter by showing how a function can actually implement a data type by itself, becoming a sort of container of its own. In fact, this is a basic theoretical point of the lambda calculus (and if you want to learn more, look up Church Encoding and Scott Encoding), so we might very well say that we have come around to the point where we began this book, at the origins of Functional Programming!

Binary trees in Haskell

Consider a binary tree. Such a tree may either be empty, or consist of a node (the tree root) with two sons: a left binary tree, and a right one.

Note

In Chapter 9, Designing Functions - Recursion, we worked with more general tree structures, such as a filesystem or the browser DOM itself, which allow a node to have any number of sons. In the particular case of the trees we are working with in...