Skip to curriculum
Jump to the editor

8.2. Curry a function of fixed length

Medium
Topics

This question is in

8. Currying and memoization
15–25 min

A curried function can take its arguments a few at a time: volume(2)(3)(4), volume(2, 3)(4) or volume(2, 3, 4) all give the same result. The calls before the last one return partials, functions with some arguments already filled in, such as a logError = log("error") that only needs a message. Utility libraries and functional code use a general curry helper so that any function can be used this way.

Write curry(fn, arity) in curry.js. It returns a curried version of fn that collects arguments until it has arity of them, then runs fn with all of them.

Signature

JAVASCRIPT
function curry(fn, arity) // returns the curried function
  • fn: the function to curry.
  • arity (optional): how many arguments to wait for. When it is missing, use fn.length, the number of parameters before the first one with a default value or a rest parameter.
  • The curried function, and every partial it returns, takes any number of arguments. It returns another function while it still waits for arguments, and fn's result once it has enough.

Requirements

  1. curry(fn) returns a function and does not call fn.
  2. Arguments can come one per call, all at once, or in any grouping: f(1)(2)(3), f(1, 2)(3), f(1)(2, 3) and f(1, 2, 3) are the same.
  3. fn does not run before it has arity arguments, and then runs exactly once, with the arguments in the order they were given, and the call returns its result.
  4. Every partial can be reused, and partials never affect each other or the curried function: each one keeps its own arguments.
  5. Arguments beyond arity in the last call are passed to fn as well.
  6. A call with no arguments adds nothing: it returns a function that still waits for the same arguments.
  7. Every value counts as an argument, undefined and null included, and an array argument stays one argument.
  8. When fn takes no arguments (arity 0), the first call runs it.
  9. arity, when given, replaces fn.length, so functions with rest or default parameters can be curried.
  10. fn runs with the this of the call that completes its arguments.

Examples

JAVASCRIPT
const volume = curry((width, height, depth) => width * height * depth);

volume(2)(3)(4);   // 24
volume(2, 3)(4);   // 24
volume(2)(3, 4);   // 24
volume(2, 3, 4);   // 24
JAVASCRIPT
const area = volume(1);
area(3)(4);        // 12
area(5, 5);        // 25: area is not changed by the call before
volume()()(2)(3)(4); // 24: empty calls add nothing
JAVASCRIPT
const total = curry((...numbers) => numbers.reduce((sum, n) => sum + n, 0), 3);
total(1)(2)(3);    // 6
total(1, 2, 3, 4); // 10: the extra argument is passed on
JAVASCRIPT
const greet = curry(function (greeting, name) {
  return greeting + ", " + name + " from " + this.city;
});
const home = { city: "Pune", greet };
home.greet("Hello", "Asha"); // "Hello, Asha from Pune"

const hello = home.greet("Hello");  // a partial
const trip = { city: "Goa", hello };
trip.hello("Asha");          // "Hello, Asha from Goa": the this of the completing call

Environment

  • Plain JavaScript in a browser page; the tests call the global curry from curry.js.
  • Placeholders, such as f(_, 2)(1), are not part of this problem: they are problem 8.3.

Hints

  1. Each partial needs its own list of arguments. Where can a list live so that it belongs to one returned function, and is never changed after?
  2. How many arguments have arrived in total, counting every call so far?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…