Functional Programming Deep Dive
Master pure functions, immutability, higher-order functions, currying, and function composition.
What you'll learn
- Pure functions and referential transparency
- Immutability and persistent data structures
- Higher-order functions: map, filter, reduce
- Currying and partial application
Concept
Functional Programming Deep Dive
Functional programming (FP) is a paradigm where programs are built by applying and composing functions. It emphasizes pure functions, immutability, and declarative style over imperative loops and mutable state.
Pure Functions
A function is pure if: (1) given the same input, it always returns the same output (no hidden state), and (2) it has no side effects (no mutation of external state, no I/O). Pure functions are easy to test, parallelize, and reason about. Referential transparency means you can replace a function call with its result without changing the program.
Immutability
In FP, you never mutate data - you create a new copy with the change. This eliminates an entire class of bugs (aliasing, race conditions) and enables cheap equality checks. Persistent data structures make this efficient through structural sharing - only the changed path is copied, the rest is shared.
Higher-Order Functions
Functions that take or return functions. The big three: map (transform each element), filter (select elements matching a predicate), reduce (fold into a single value). These replace loops and make data transformations declarative.
Currying
Transforming a function f(a, b, c) into f(a)(b)(c) - a function that takes one argument and returns a function taking the next. This enables partial application: fix some arguments to create a specialized function.
Composition
Combining functions into pipelines: (f compose g)(x) = f(g(x)). Composition is the FP equivalent of chaining methods - it builds complex behavior from simple, reusable pieces.
Example
// Pure functions - no side effects
const add = (a, b) => a + b;
// Currying
const curry = (fn) => (a) => (b) => fn(a, b);
const curriedAdd = curry(add);
const add10 = curriedAdd(10);
console.log(add10(5)); // 15
// Composition: pipe functions left-to-right
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);
const double = (x) => x * 2;
const increment = (x) => x + 1;
const square = (x) => x * x;
const transform = pipe(double, increment, square);
console.log(transform(3)); // (3*2 + 1)^2 = 49
// Immutable update
const updateAt = (arr, idx, val) => [...arr.slice(0, idx), val, ...arr.slice(idx + 1)];
const nums = [1, 2, 3, 4, 5];
const updated = updateAt(nums, 2, 99);
console.log('Original:', nums);
console.log('Updated:', updated);
// Declarative pipeline
const result = [1,2,3,4,5,6,7,8,9,10].filter(x => x % 2 === 0).map(x => x * x).reduce((s, x) => s + x, 0);
console.log('Pipeline result:', result);
curry transforms a 2-arg function into a chain of 1-arg functions. pipe composes functions left-to-right. updateAt returns a new array without mutating.
Try it
- JSON Formatter
Formatting is a pure function - same input always gives same output. A functional programming example.
Common mistakes
The mistake
Calling map for side effects instead of using forEach.
The fix
map builds a new array. If you do not use the result, you are wasting memory. Use forEach for side-effectful iteration.
The mistake
Mutating arguments inside a function, breaking purity.
The fix
A function that modifies its input argument is not pure. Return a new object with the changes instead.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.