8.2. Curry a function of fixed length
Topics
This question is in
8. Currying and memoizationA 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
function curry(fn, arity) // returns the curried functionfn: the function to curry.arity(optional): how many arguments to wait for. When it is missing, usefn.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
curry(fn)returns a function and does not callfn.- 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)andf(1, 2, 3)are the same. fndoes not run before it hasarityarguments, and then runs exactly once, with the arguments in the order they were given, and the call returns its result.- Every partial can be reused, and partials never affect each other or the curried function: each one keeps its own arguments.
- Arguments beyond
arityin the last call are passed tofnas well. - A call with no arguments adds nothing: it returns a function that still waits for the same arguments.
- Every value counts as an argument,
undefinedandnullincluded, and an array argument stays one argument. - When
fntakes no arguments (arity0), the first call runs it. arity, when given, replacesfn.length, so functions with rest or default parameters can be curried.fnruns with thethisof the call that completes its arguments.
Examples
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); // 24const 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 nothingconst 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 onconst 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 callEnvironment
- Plain JavaScript in a browser page; the tests call the global
curryfromcurry.js. - Placeholders, such as
f(_, 2)(1), are not part of this problem: they are problem 8.3.
Hints
- 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?
- 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