How can you overcome JavaScript language oddities and unsafe features? With this book, you’ll learn how to create code that’s beautiful, safe, and simple to understand and test by using JavaScript’s functional programming support. Author Michael Fogus shows you how to apply functional-style concepts with Underscore.js, a JavaScript library that facilitates functional programming techniques. Sample code is available on GitHub at https://github.com/funjs/book-source.
Fogus helps you think in a functional way to help you minimize complexity in the programs you build. If you’re a JavaScript programmer hoping to learn functional programming techniques, or a functional programmer looking to learn JavaScript, this book is the ideal introduction.
- Use applicative programming techniques with first-class functions
- Understand how and why you might leverage variable scoping and closures
- Delve into higher-order functions—and learn how they take other functions as arguments for maximum advantage
- Explore ways to compose new functions from existing functions
- Get around JavaScript’s limitations for using recursive functions
- Reduce, hide, or eliminate the footprint of state change in your programs
- Practice flow-based programming with chains and functional pipelines
- Discover how to code without using classes
Author(s): Michael Fogus
Publisher: O'Reilly Media
Year: 2013
Language: English
Pages: 260
Copyright......Page 4
Table of Contents......Page 7
Foreword by Jeremy Ashkenas......Page 11
Foreword by Steve Vinoski......Page 13
Using Underscore......Page 15
The Source Code for Functional JavaScript......Page 16
Whom Functional JavaScript Is Written For......Page 17
A Roadmap for Functional JavaScript......Page 18
Conventions Used in This Book......Page 19
Safari® Books Online......Page 20
Acknowledgments......Page 21
The Case for JavaScript......Page 23
Some Limitations of JavaScript......Page 25
Why Functional Programming Matters......Page 26
Functions as Units of Abstraction......Page 30
Encapsulation and Hiding......Page 32
Functions as Units of Behavior......Page 33
Data as Abstraction......Page 37
A Taste of Functional JavaScript......Page 41
On Speed......Page 43
The Case for Underscore......Page 46
Summary......Page 47
Functions as First-Class Things......Page 49
JavaScript’s Multiple Paradigms......Page 51
Applicative Programming......Page 56
Collection-Centric Programming......Page 57
Other Examples of Applicative Programming......Page 58
Defining a Few Applicative Functions......Page 61
Data Thinking......Page 63
“Table-Like” Data......Page 65
Summary......Page 69
Global Scope......Page 71
Lexical Scope......Page 73
Dynamic Scope......Page 74
JavaScript’s Dynamic Scope......Page 77
Function Scope......Page 78
Closures......Page 81
Simulating Closures......Page 82
Using Closures......Page 87
Summary......Page 89
Functions That Take Other Functions......Page 91
Thinking About Passing Functions: max, finder, and best......Page 92
More Thinking About Passing Functions: repeat, repeatedly, and
iterateUntil......Page 94
Functions That Return Other Functions......Page 97
Capturing Variables for Great Good......Page 99
A Function to Guard Against Nonexistence: fnull......Page 102
Putting It All Together: Object Validators......Page 104
Summary......Page 107
The Essence of Functional Composition......Page 109
Mutation Is a Low-Level Operation......Page 113
Currying......Page 114
To Curry Right, or To Curry Left......Page 116
Automatically Currying Parameters......Page 117
Currying for Fluent APIs......Page 121
Partial Application......Page 122
Partially Applying One and Two Known Arguments......Page 124
Partially Applying an Arbitrary Number of Arguments......Page 125
Partial Application in Action: Preconditions......Page 126
Stitching Functions End-to-End with Compose......Page 130
Pre- and Postconditions Using Composition......Page 131
Summary......Page 132
Self-Absorbed Functions (Functions That Call Themselves)......Page 135
Graph Walking with Recursion......Page 140
Depth-First Self-Recursive Search with Memory......Page 141
Recursion and Composing Functions: Conjoin and Disjoin......Page 144
Codependent Functions (Functions Calling Other Functions That Call
Back)......Page 146
Deep Cloning with Recursion......Page 147
Walking Nested Arrays......Page 148
Too Much Recursion!......Page 151
Generators......Page 153
The Trampoline Principle and Callbacks......Page 156
Recursion Is a Low-Level Operation......Page 158
Summary......Page 159
Purity......Page 161
The Relationship Between Purity and Testing......Page 162
Separating the Pure from the Impure......Page 164
Property-Testing Impure Functions......Page 165
Purity and the Relationship to Referential Transparency......Page 166
Purity and the Relationship to Idempotence......Page 168
Immutability......Page 169
If a Tree Falls in the Woods, Does It Make a Sound?......Page 171
Immutability and the Relationship to Recursion......Page 172
Defensive Freezing and Cloning......Page 173
Observing Immutability at the Function Level......Page 175
Observing Immutability in Objects......Page 177
Objects Are Often a Low-Level Operation......Page 181
Policies for Controlling Change......Page 182
Summary......Page 185
Chaining......Page 187
A Lazy Chain......Page 190
Promises......Page 195
Pipelining......Page 198
Data Flow versus Control Flow......Page 202
Finding a Common Shape......Page 205
A Function to Simplify Action Creation......Page 209
Summary......Page 211
Data Orientation......Page 213
Building Toward Functions......Page 216
Mixins......Page 220
Core Prototype Munging......Page 222
Class Hierarchies......Page 223
Changing Hierarchies......Page 226
Flattening the Hierarchy with Mixins......Page 227
New Semantics via Mixin Extension......Page 233
New Types via Mixin Mixing......Page 234
Methods Are Low-Level Operations......Page 236
}).call(“Finis”);......Page 238
Functional JavaScript......Page 239
Underscore-contrib......Page 240
RxJS......Page 241
Bilby......Page 242
allong.es......Page 243
Other Functional Libraries......Page 244
ClojureScript......Page 245
Roy......Page 246
Elm......Page 247
Books......Page 249
Blog Posts......Page 251
Journal Articles......Page 252
Index......Page 253
About the Author......Page 260