Excerpts from Functional Programming in JavaScript
Chapter 1
- Identify abstractions and build functions for them;
- Use existing functions to build more complex abstractions;
- Build even more complex abstractions by passing existing functions to other functions.
Chapter 1
- Identify abstractions and build functions for them;
- Use existing functions to build more complex abstractions;
- Build even more complex abstractions by passing existing functions to other functions.
Chapter 2
Functions as first-class citizens:
Functional programming languages should facilitate the creation and use of functions;
Applicative programming:
Function A is provided as an argument to function B;
JavaScript object keys can only be strings;
Chapter 3
Lexical scope:
Refers to a variable’s visibility and the simulated value of its textual representation; variable lookup expands outward from the innermost scope.
Dynamic scope:
- Maintains a global table of “values” (a global mapping of named binding stacks);
- Uses a function to look up the bound value;
- Drawback: for any given binding value, it is unknowable until the function calling it is determined.
Closure:
A closure is a function that captures external bindings in its scope (i.e., not its own arguments). These bindings are defined for later use (even after the scope has ended).
If a variable reference exists both inside and outside a closure, its changes can cross seemingly private boundaries. Therefore, JavaScript often uses the following pattern, treating captured variables as private data:
1 | var pingpong = (function() { |
Chapter 4
Higher-order functions:
- They are first-class citizens;
- Take a function as an argument;
- Return a function as a result;
Closure:
- Closures capture a value (or reference) and return the same value multiple times;
- Each new closure captures a different value;
Chapter 5
Curried functions:
Gradually return functions that consume arguments until all arguments are exhausted;
Partial application:
A partially executed function that waits to receive the remaining arguments for immediate execution;
Function composition:
_.compose: executes from right to left, the result of the rightmost function is fed to the function on its left, one after another.
Chapter 6
Tail recursion:
The last action of a function (besides the stopping condition return value) is a recursive call;
Recursion and function composition:
andify and orify;
Mutually related functions:
Two or more functions calling each other is called mutual recursion. They bounce back and forth through mutual recursive calls, decrementing an absolute value until one side or the other reaches zero;
Trampoline principle:
By wrapping calls rather than calling directly; the trampoline function: continuously calls the return value of the function until it is no longer a function.
Chapter 7
Pure functions:
- Their result can only be computed from their argument values;
- They cannot depend on data that can be changed by external operations;
- They cannot change external state.
Idempotency:
Running a function once with the same arguments should produce the same result as running it twice consecutively.
Immutability:
For example, strings;
Object immutability:
- Immutable objects should fix their values at construction and not be modifiable afterward;
- Immutable object operations return new objects.
Chapter 8
Lazy chains:
Before calling value, _.chain is lazy—nothing happens;
Thunk:
A function that wraps some behavior;
Pipeline;
Excerpts from Functional Programming in JavaScript
http://quanru.github.io/2015/07/19/Excerpts-from-Functional-Programming-in-JavaScript

