Lesson 4 of 9 · 19m
Currying and Partial Application With Purpose
When currying clarifies configuration-first pipelines, when it makes call sites worse, and how to implement it without magic.
Currying looks trivial in a blog post and then ruins a codebase when applied indiscriminately. A curried function takes one argument at a time and returns the next function. Used well, it separates stable configuration from varying data. Used badly, it makes stack traces longer and call sites impossible to search.
The useful shape
const fetchJson = (baseUrl) => (path) =>
fetch(`${baseUrl}${path}`).then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
});
const api = fetchJson("https://billing.internal");
const invoices = api("/v1/invoices");
const customers = api("/v1/customers");baseUrl is fixed once; path varies per call. That is the practical split currying is good at. Arithmetic examples like add(2)(3) demonstrate the mechanism but not the reason.
Partial application is the less rigid cousin
function partial(fn, ...fixed) {
return (...rest) => fn(...fixed, ...rest);
}
const post = partial(request, "POST");
post("/v1/users", { body: payload });JavaScript APIs are positional and often variadic. Partial application is often friendlier than strict one-at-a-time currying. bind can do it too, but it also controls this, which is usually a distraction in functional code.
Currying and TypeScript do not always get along
A tiny explicit function is often clearer than a maximally generic helper:
const withTeam = (teamId) => (query) => ({ ...query, teamId });A magic curry3 with overloads and rest tuples may be intellectually satisfying. It can also make inference, autocomplete, and jump-to-definition worse for every caller. That is a real cost, not a style preference.
Do not curry everything
Bad signs include saveUser(db)(logger)(clock)(input) everywhere, unnamed positional arguments at invocation, and configuration separated from the final call by many layers. Prefer an options object when there are several independent settings. Currying is strongest for one stable dependency plus one varying input.
Field notes
- Put the most stable argument first and the most variable data last.
- Curried functions allocate intermediate closures. Usually irrelevant, occasionally real in tight loops.
- Concise arrow chains are easy to write and hard to audit. Readability wins.
- If searching for callers becomes harder, the abstraction is costing more than it saves.
Was this lesson helpful?