Functional Programming and Currying
Build a precise mental model of JavaScript values, scope, functions, objects, arrays and asynchronous behavior, then implement and debug real utilities from first principles instead of memorizing outputs.
What you will be able to do
#- Write pure functions and recognize where side effects belong.
- Compose small functions with pipe and compose.
- Implement currying and partial application.
Overview
#Pure functions
A pure function returns the same output for the same input and changes nothing outside itself. Purity makes code easy to test and reason about, because the result depends only on the arguments. Side effects such as network calls and logging are pushed to the edges of the program.
Composition
map, filter and reduce turn a loop into a pipeline of small steps. pipe runs functions left to right and compose runs them right to left. Each step should do one thing so the pipeline reads like a sentence.
const pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x);
const double = (n) => n * 2;
const inc = (n) => n + 1;
console.log(pipe(double, inc)(3)); // 7
Currying and partial application
A curried function takes one argument at a time and returns a function until all arguments are supplied. Partial application fixes some arguments and returns a function for the rest. Both let you build specialized functions from general ones and are the basis of point-free pipelines.
const curry = (fn) => {
const arity = fn.length;
const build = (...args) => args.length >= arity ? fn(...args) : (...more) => build(...args, ...more);
return build;
};
const add = curry((a, b) => a + b);
console.log(add(1)(2)); // 3
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this line print?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.javascriptconst curry = (fn) => (a) => (b) => fn(a, b); const add = curry((a, b) => a + b); console.log(add(2)(3), add(2)(3) === 5);Exercise 2Explain
Explain the difference between currying and partial application.
starter.textconst add3 = (a) => (b) => (c) => a + b + c;Reveal the reference answer
reference.textCurrying rewrites a function of several arguments into a chain of functions that each take one argument, so add3(a)(b)(c). Partial application fixes some of the arguments now and returns a function for the rest, which may still take several arguments at once. Currying is a transformation of the function; partial application is a way of calling it. A curried function makes partial application easy.Authored expected output: a function of one argument at a time
The answer should separate the transformation from the act of supplying a subset of arguments.
Exercise 3Implement
Implement `compose(...fns)` so it applies functions right to left. Running `console.log(compose((n) => n + 1, (n) => n * 2)(3))` should print `7`.
starter.javascriptfunction compose(...fns) { // your code here }Reveal the reference answer
reference.javascriptfunction compose(...fns) { return (x) => fns.reduceRight((acc, fn) => fn(acc), x); }Authored expected output: 7
reduceRight applies the rightmost function first, so 3 is doubled to 6 and then incremented to 7, matching the mathematical order of composition.
Exercise 4Debug
This sum is meant to total an array of numbers but throws on an empty array. Find and fix the bug.
starter.javascriptfunction sum(nums) { return nums.reduce((acc, n) => acc + n); } console.log(sum([]));Reveal the reference answer
reference.javascriptfunction sum(nums) { return nums.reduce((acc, n) => acc + n, 0); } console.log(sum([]));Authored expected output: 0
reduce without an initial value uses the first element as the seed and throws on an empty array. Passing 0 makes the empty case return 0 and removes the edge case.
Exercise 5Test
Write assertions that prove pipe is left to right and that a pure function does not mutate its input.
starter.javascriptfunction assertEqual(actual, expected, label) { if (JSON.stringify(actual) !== JSON.stringify(expected)) throw new Error(label + ': unexpected value'); }Reveal the reference answer
reference.javascriptconst pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x); assertEqual(pipe((n) => n + 1, (n) => n * 2)(3), 8, 'pipe runs left to right'); const input = [1, 2, 3]; const doubled = input.map((n) => n * 2); assertEqual(input, [1, 2, 3], 'map does not mutate the source'); assertEqual(doubled, [2, 4, 6], 'map returns a new array'); console.log('all assertions passed');Authored expected output: all assertions passed
pipe applies the leftmost function first, so 3 becomes 4 then 8. map returns a new array and leaves the input unchanged, which is what makes the pipeline predictable.
Exercise 6Interview
Where would functional patterns help in a real frontend codebase, and where would they hurt?
Reveal the reference answer
reference.textThey help for data transformation, derived state and building reusable pipelines from small pure helpers, because those are easy to test. They hurt when taken to an extreme: deep point-free chains become unreadable, and forcing purity around inherently effectful work such as network calls or DOM updates adds indirection without benefit. I use pure functions for logic and keep effects at the boundary, and I favor readability over cleverness.The answer should name concrete wins such as testable transformations and concrete costs such as unreadable point-free code and forced purity.
Common misconceptions
#- reduce without an initial value is safe for every array.
- Currying and partial application are the same thing.
- A function that logs to the console is still pure.
Follow-up practice
#- javascript-foundations-recursion
- javascript-foundations-arrays-and-iteration
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.