Cover....1
Title Page....2
Copyright and Credits....3
Contributors....5
About the reviewers....6
Table of Contents....8
Preface....14
Chapter 1: Becoming Functional – Several Questions....22
What is functional programming?....22
Theory versus practice....23
A different way of thinking....23
FP and other programming paradigms....24
What FP is not....25
Why use FP?....26
What we need....26
What we get....27
Not all is gold....27
Is JavaScript functional?....28
JavaScript as a tool....28
Going functional with JavaScript....29
Key features of JavaScript....30
How do we work with JavaScript?....36
Using transpilers....38
Working online....40
A step further – TypeScript....40
Testing....43
Summary....43
Questions....43
Chapter 2: Thinking Functionally – A First Example....46
Our problem – doing something only once....46
Solution 1 – hoping for the best!....47
Solution 2 – using a global flag....48
Solution 3 – removing the handler....49
Solution 4 – changing the handler....50
Solution 5 – disabling the button....50
Solution 6 – redefining the handler....51
Solution 7 – using a local flag....51
A functional solution to our problem....52
A higher-order solution....53
Testing the solution manually....56
Testing the solution automatically....56
Producing an even better solution....58
Summary....60
Questions....61
Chapter 3: Starting Out with Functions – A Core Concept....62
All about functions....62
Of lambdas and functions....63
Arrow functions – the modern way....66
Functions as objects....71
Using functions in FP ways....77
Injection – sorting it out....77
Callbacks and promises....81
Continuation-passing style....82
Polyfills....83
Stubbing....87
Immediate invocation (IIFE)....88
Summary....91
Questions....92
Chapter 4: Behaving Properly – Pure Functions....94
Pure functions....94
Referential transparency....95
Side effects....97
Advantages of pure functions....104
Impure functions....109
Avoiding impure functions....110
Is your function pure?....116
Testing – pure versus impure....118
Testing pure functions....118
Testing purified functions....120
Testing impure functions....124
Summary....128
Questions....128
Chapter 5: Programming Declaratively – A Better Style....132
Transformations....133
Reducing an array to a value....133
Applying an operation – map()....141
Dealing with arrays of arrays....148
More general looping....156
Logical HOFs....158
Filtering an array....158
Searching an array....161
Higher-level predicates – every() and some()....164
Checking negatives – none()....165
Working with async functions....166
Some strange behaviors....167
Async-ready looping....169
Working with parallel functions....174
Unresponsive pages....174
A frontend worker....177
A backend worker....180
Workers, FP style....181
Long-living pooled workers....183
Summary....186
Questions....187
Chapter 6: Producing Functions – Higher-Order Functions....190
Wrapping functions – keeping behavior....190
Logging....191
Timing functions....201
Memoizing functions....203
Altering a function’s behavior....214
Doing things once, revisited....214
Logically negating a function....217
Inverting the results....219
Arity changing....221
Throttling and debouncing....223
Changing functions in other ways....225
Turning operations into functions....226
Turning functions into promises....229
Getting a property from an object....231
Demethodizing – turning methods into functions....233
Methodizing – turning functions into methods....236
Finding the optimum....240
Summary....243
Questions....243
Chapter 7: Transforming Functions – Currying and Partial Application....246
A bit of theory....246
Currying....247
Dealing with many parameters....248
Currying by hand....251
Currying with bind()....253
Partial application....257
Partial application with arrow functions....258
Partial application with closures....259
Partial currying....268
Partial currying with bind()....270
Partial currying with closures....273
Final thoughts....274
Variable number of parameters....275
Parameter order....275
Being functional....277
Summary....278
Questions....279
Chapter 8: Connecting Functions – Pipelining, Composition, and More....282
Pipelining....282
Piping in Unix/Linux....283
Revisiting an example....284
Creating pipelines....286
Debugging pipelines....294
Pointfree style....297
Chaining and fluent interfaces....300
An example of fluent APIs....300
Chaining method calls....302
Composing....305
Some examples of composition....306
Composing with higher-order functions....310
Transducing....316
Composing reducers....319
Generalizing for all reducers....321
Testing connected functions....322
Testing pipelined functions....322
Testing composed functions....325
Testing chained functions....326
Testing transduced functions....328
Summary....330
Questions....330
Chapter 9: Designing Functions – Recursion....334
Using recursion....334
Thinking recursively....336
Higher-order functions revisited....346
Searching and backtracking....354
Mutual recursion....362
Odds and evens....362
Doing arithmetic....364
Recursion techniques....369
Tail call optimization....369
Continuation-passing style....372
Trampolines and thunks....377
Recursion elimination....380
Summary....381
Questions....381
Chapter 10: Ensuring Purity – Immutability....386
Going the straightforward JavaScript way....387
Mutator functions....387
Constants....388
Freezing....389
Cloning and mutating....392
Getters and setters....396
Lenses....401
Prisms....413
Creating persistent data structures....417
Working with lists....417
Updating objects....419
A final caveat....425
Summary....425
Questions....426
Chapter 11: Implementing Design Patterns – The Functional Way....430
Understanding design patterns....430
Design pattern categories....431
Do we need design patterns?....433
Object-oriented design patterns....433
Facade and Adapter....434
Decorator or Wrapper....437
Strategy, Template, and Command....441
Dependency Injection....443
Observers and reactive programming....446
Other patterns....458
Functional design patterns....458
Summary....460
Questions....460
Chapter 12: Building Better Containers – Functional Data Types....464
Specifying data types....464
Signatures for functions....465
Other data type options....468
Building containers....470
Extending current data types....471
Containers and functors....473
Monads....488
Functions as data structures....497
Binary trees in Haskell....498
Functions as binary trees....499
Summary....507
Questions....508
Answers to Questions....512
Chapter 1, Becoming Functional – Several Questions....512
Chapter 2, Thinking Functionally – A First Example....518
Chapter 3, Starting Out with Functions – A Core Concept....524
Chapter 4, Behaving Properly – Pure Functions....528
Chapter 5, Programming Declaratively – A Better Style....535
Chapter 6, Producing Functions – Higher-Order Functions....548
Chapter 7, Transforming Functions – Currying and Partial Application....557
Chapter 8, Connecting Functions – Pipelining, Composition, and More....562
Chapter 9, Designing Functions – Recursion....565
Chapter 10, Ensuring Purity – Immutability....575
Chapter 11, Implementing Design Patterns – The Functional Way....583
Chapter 12, Building Better Containers – Functional Data Types....586
Bibliography....598
Index....600
Other Books You May Enjoy....611
Functional programming is a programming paradigm that uses functions for developing software. This book is filled with examples that enable you to leverage the latest JavaScript and TypeScript versions to produce modern and clean code, as well as teach you to how apply functional programming techniques to develop more efficient algorithms, write more concise code, and simplify unit testing.
This book provides comprehensive coverage of the major topics in functional programming to produce shorter, clearer, and testable programs. You'll begin by getting to grips with writing and testing pure functions, reducing side effects, as well as other key features to make your applications functional in nature. The book specifically explores techniques to simplify coding, apply recursion, perform high-level 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 practical programming skills needed to confidently enhance your applications by adding functional programming to wherever it's most suitable.
If you are a JavaScript or TypeScript developer looking to enhance your programming skills, then this book is for you. The book applies to both frontend developers working with frameworks such as React, Vue, or Angular as well as backend developers using Node.js or Deno.