Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering JavaScript Functional Programming

You're reading from   Mastering JavaScript Functional Programming Write clean, robust, and maintainable web and server code using functional JavaScript and TypeScript

Arrow left icon
Product type Paperback
Published in Apr 2023
Publisher Packt
ISBN-13 9781804610138
Length 614 pages
Edition 3rd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Federico Kereki Federico Kereki
Author Profile Icon Federico Kereki
Federico Kereki
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Becoming Functional – Several Questions 2. Chapter 2: Thinking Functionally – A First Example FREE CHAPTER 3. Chapter 3: Starting Out with Functions – A Core Concept 4. Chapter 4: Behaving Properly – Pure Functions 5. Chapter 5: Programming Declaratively – A Better Style 6. Chapter 6: Producing Functions – Higher-Order Functions 7. Chapter 7: Transforming Functions – Currying and Partial Application 8. Chapter 8: Connecting Functions – Pipelining, Composition, and More 9. Chapter 9: Designing Functions – Recursion 10. Chapter 10: Ensuring Purity – Immutability 11. Chapter 11: Implementing Design Patterns – The Functional Way 12. Chapter 12: Building Better Containers – Functional Data Types 13. Answers to Questions 14. Bibliography
15. Index 16. Other Books You May Enjoy

Functional design patterns

Having seen several OOP design patterns, it may seem a cheat to say that there’s no approved, official, or even remotely generally accepted similar list of patterns for FP. There are, however, several problems for which there are standard FP solutions, which can be considered design patterns on their own, and we have already covered most of them in this book.

What are the candidates for a possible list of patterns? Let’s attempt to prepare one – but remember that it’s just a personal view. Also, I’ll admit that I’m not trying to mimic the usual style of pattern definition; I’ll just be mentioning a general problem and refer to the way FP in JavaScript can solve it, and I won’t be aiming for nice, short, and memorable names for the patterns either:

  • Processing collections using filter/map/reduce: Whenever you have to process a data collection, using declarative higher-order functions such as filter(), map(), and reduce(), as we saw in this chapter and previously in Chapter 5, Programming Declaratively, is a way to remove complexity from the problem. (The usual MapReduce web framework is an extension of this concept, which allows for distributed processing among several servers, even if the implementation and details aren’t exactly the same.) Instead of performing looping and processing as a single step, you should think about the problem as a sequence of steps, applied in order, and doing transformations until obtaining the final, desired result.

Looping in other ways

JavaScript also includes iterators, another way of looping through a collection. Using iterators isn’t particularly functional, but you may want to look at them, since they may be able to simplify some situations. Read more at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols.

  • Lazy evaluation with thunks: The idea of lazy evaluation is not doing any calculations until they are actually needed. In some programming languages, this is built in. However, in JavaScript (and most imperative languages), eager evaluation is applied, in which an expression is evaluated as soon as it is bound to some variable. (Another way of saying this is that JavaScript is a strict programming language, with a strict paradigm, which only allows calling a function if all of its parameters have been completely evaluated.) This sort of evaluation is required when you need to specify the order of evaluation with precision, mainly because such evaluations may have side effects.

In FP, which is more declarative and pure, you can delay such evaluation with thunks (which we used in the Trampolines and thunks section of Chapter 9, Designing Functions) by passing a thunk that will calculate the needed value only when it’s needed, but not earlier.

Generating more results

You may also want to look at JavaScript generators, another way of delaying evaluation, though not particularly related to FP. Read more about them at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator. The combination of generators and promises is called an async function, which may be of interest to you; refer to developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function.

  • Persistent data structures for immutability: Having immutable data structures, as we saw in Chapter 10, Ensuring Purity, is mandatory when working with certain frameworks, and in general, it is recommended because it helps to reason about a program or to debug it. (Earlier in this chapter, we also mentioned how the Memento OOP pattern can be implemented in this fashion.) Whenever you have to represent structured data, the FP solution of using a persistent data structure helps in many ways.
  • Wrapped values for checks and operations: If you directly work with variables or data structures, you can modify them at will (possibly violating any restrictions), or you may need to do many checks before using them (such as verifying that a value is not null before trying to access the corresponding object). This pattern aims to wrap a value within an object or function, so direct manipulation won’t be possible, and checks can be managed more functionally. We’ll refer to more of this in Chapter 12, Building Better Containers.

As we have said, the power of FP is such that, instead of having a couple of dozen standard design patterns (and that’s only in the GoF book; if you read other texts, the list grows!), there isn’t yet a standard or acknowledged list of functional patterns.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image