Curry, Compose & Memoize in JavaScript
Three utilities turn functions into building blocks: currying transforms f(a,b) into f(a)(b) (specialize incrementally), compose/pipe chains f(g(x)) left-to-right or right-to-left (data pipelines without intermediate variables), and memoize caches results by arguments (pure functions only — impure ones go stale).
Each is laughably short to write and profoundly reusable: curried handlers pre-fill config, composed validators read as sentences, memoized recursion (fibonacci!) drops exponential to linear. Libraries (lodash/fp, Ramda) industrialize the patterns.
Curry specializes gradually
const add = a => b => a + b; add(2)(3) — one argument now, the rest later, reusable partials in between.
Compose chains right-to-left
compose(f, g)(x) is f(g(x)); pipe flows left-to-right — pipelines without temporary variables.
Memoize caches pure calls
Cache keyed by JSON args returns stored results — same inputs must always give same outputs, or stale lies.
Purity is prerequisite
Memoizing impure functions (random, I/O, dates) freezes one arbitrary result — pure functions only.
let add = (a) => (b) => a + b;let add5 = add(5);console.log(add5(3));let shout = (s) => s.toUpperCase() + "!";console.log(shout("hi"));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.