-
Book Overview & Buying
-
Table Of Contents
Mastering JavaScript Functional Programming - Third Edition
By :
So far, we have learned how to use functions to work with or transform other functions to process data structures or to create data types. Now, we’ll finish this chapter by showing you how a function can implement a data type, becoming a container of its own. In fact, this is a fundamental theoretical point of lambda calculus (if you want to learn more, look up Church encoding and Scott encoding), so we may very well say that we have come back to where we began this book, to the origins of FP! We will start with a detour that considers binary trees in a different functional language, Haskell, and then move on to implementing trees as functions, but in JavaScript. This experience will help you work out how to deal with other data structures.
Consider a binary tree. Such a tree may either be empty or consist of a node (the tree root) with two children: a left binary tree and a right one. A node that has no children is...