JS Built-ins
FunctionalProgrammingadvanced

Pure function composition

πŸ“– Usage Example
const add = a => b => a + b;
const double = x => x * 2;
const addThenDouble = x => double(add(0)(x));
addThenDouble(5); // 10

Pure functions compose cleanly because they're self-contained with no hidden state.

Composing Pure Functions

One benefit of pure functions is that they compose reliably. You can chain them together without worrying about side effects.

Challenge: Compose two pure functions: add(a, b) and double(x). Implement addThenDouble which takes two numbers, adds them, then doubles the result.

πŸ’‘ Hints (3)
  • β€’ Call add() first with both arguments
  • β€’ Pass the result of add() to double()
  • β€’ Composition of pure functions is also pure