Functional Light v3
Functional Light v3
COM
FUNCTIONAL-LIGHT JS
github.com/getify/Functional-Light-JS
WHY FP?
IMPERATIVE
VS
DECLARATIVE
PROVABLE
LESS TO READ
Course Overview
• Functions
• Closure
• Composition
• Immutability
• Recursion
• Lists / Data Structures
• Async
• FP Libraries
...but before we begin...
FUNCTIONS
Procedures
Functions
Function: the semantic relationship
between input and computed output
SIDE EFFECTS
Avoid side effects with
function calls...
if possible
Side Effects:
• I / O (console, files, etc)
• Database Storage
• Network Calls
• DOM
• Timestamps
• Random Numbers
• CPU Heat
• CPU Time Delay
• etc
No such thing as
"no side effects"
Avoid them where possible,
make them obvious otherwise
PURE FUNCTIONS
Function Purity:
Level of Confidence
Function Purity:
Calls, Not Definitions
EXTRACTING IMPURITY
CONTAINING IMPURITY
1.
2.
3.
4.
5.
6.
ARGUMENTS
unspread(..)?
POINT-FREE
Equational Reasoning
Shape
Advanced Point-Free
CLOSURE
Closure is when a function
"remembers" the variables around
it even when that function is
executed elsewhere.
LAZY VS EAGER
Memoization
GENERALIZED
TO SPECIALIZED
Function Parameter Order:
General -> Specific
PARTIAL APPLICATION
CURRYING
Partial Application vs Currying:
1. Both are specialization techniques
2. Partial Application presets some arguments
now, receives the rest on the next call
3. Currying doesn't preset any arguments,
receives each argument one at a time
Specialization Adapts Shape
COMPOSITION
(RIGHT-TO-LEFT)
COMPOSE: RIGHT-TO-LEFT
PIPE: LEFT-TO-RIGHT
ASSOCIATIVITY
CURRYING REVISITED
POINT-FREE REVISITED
IMMUTABILITY
ASSIGNMENT
IMMUTABILITY
VALUE
IMMUTABILITY
Read-Only Data Structures:
Data structures that never
need to be mutated
Treat all data structures as
read-only whether they are
or not
IMMUTABLE DATA
STRUCTURES
facebook.github.io/immutable-js
Immutable Data Structures:
Data structures that need to
be mutated
RECURSION
PTC
PROPER TAIL CALLS
CPS
TRAMPOLINES
LISTS
actually, data structures
MAP: TRANSFORMATION
FILTER: EXCLUSION
ACTUALLY, INCLUSION?
REDUCE: COMBINING
COMPOSITION
REVISITED
FUSION
TRANSDUCING
DERIVING
TRANSDUCTION
DATA STRUCTURE
OPERATIONS
MONAD:
FP DATA STRUCTURE
Monad: a monoid in the category of
endofunctors
FUNCTIONAL-LIGHT JS