[go: up one dir, main page]

0% found this document useful (0 votes)
427 views188 pages

Functional Light v3

The document discusses functional programming concepts including: 1. Functions and avoiding side effects when possible through pure functions and extracting or containing impurity. 2. Closure and how functions "remember" variables even when executed elsewhere. 3. Composition by combining functions from right to left and associativity. 4. Immutability through value immutability, read-only data structures, and immutable data structure libraries. 5. Recursion through proper tail calls, continuation passing style, and trampolines.

Uploaded by

jowke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
427 views188 pages

Functional Light v3

The document discusses functional programming concepts including: 1. Functions and avoiding side effects when possible through pure functions and extracting or containing impurity. 2. Closure and how functions "remember" variables even when executed elsewhere. 3. Composition by combining functions from right to left and associativity. 4. Immutability through value immutability, read-only data structures, and immutable data structure libraries. 5. Recursion through proper tail calls, continuation passing style, and trampolines.

Uploaded by

jowke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 188

KYLE SIMPSON GETIFY@GMAIL.

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

Monad: a pattern for pairing data with a


set of predictable behaviors that let it
interact with other data+behavior
pairings (monads)
There are many kinds of monads:
Just, Nothing, Maybe, Either, IO, etc

Should you use monads?


Maybe.
ASYNC
SYNCHRONOUS, EAGER FP
LAZY FP,
OVER TIME?
"LAZYARRAY"

≅

OBSERVABLE
FP LIBRARIES
LODASH/FP
github.com/lodash/lodash/wiki/FP-Guide
RAMDA
ramdajs.com
github.com/getify/fpo
RECAP:
‣ Functions (side effects, point-free)
‣ Closure
‣ Composition
‣ Immutability
‣ Recursion
‣ Lists & Data Structures
‣ Async (observables)
THANKS!!!!

KYLE SIMPSON GETIFY@GMAIL.COM

FUNCTIONAL-LIGHT JS

You might also like