Learn JavaScript
❯ functions.functional_utils

Curry, Compose & Memoize in JavaScript

$concept

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.

$how it works
1

Curry specializes gradually

const add = a => b => a + b; add(2)(3) — one argument now, the rest later, reusable partials in between.

2

Compose chains right-to-left

compose(f, g)(x) is f(g(x)); pipe flows left-to-right — pipelines without temporary variables.

3

Memoize caches pure calls

Cache keyed by JSON args returns stored results — same inputs must always give same outputs, or stale lies.

4

Purity is prerequisite

Memoizing impure functions (random, I/O, dates) freezes one arbitrary result — pure functions only.

$example
let add = (a) => (b) => a + b;
let add5 = add(5);
console.log(add5(3));
let shout = (s) => s.toUpperCase() + "!";
console.log(shout("hi"));
Output
8
hi!
1add(5) returns a specialized adder closing over a=5.
2add5(3) completes to 8 — curried specialization in action.
3Small composable units combine into larger behavior without intermediaries.
$use cases
Pre-filled handlers (onChange with field names curried in)
Validation/transformation pipelines via composition
Memoized recursion and expensive pure computations
Lodash/fp and Ramda patterns in production code

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.