8.3. Curry with placeholders
Topics
This question is in
8. Currying and memoizationA curried function takes its arguments in order, so a partial can only fix the first ones. Often the one you want to fix is later: a divide(a, b) where you know b (halve = divide(_, 2)), or a request(method, url, options) where the options are the same for every call. A placeholder solves this: it holds a position open, and a later call fills it.
Write curry(fn, arity) in curry.js. It returns a curried version of fn that collects arguments over any number of calls, where the value curry.placeholder skips a position. Once the first arity positions all hold real values, it runs fn.
Signature
function curry(fn, arity) // returns the curried function
curry.placeholder // a Symbol, defined at the bottom of curry.jsfn: the function to curry.arity(optional): how many positions must be filled beforefnruns. When it is missing, usefn.length.- The curried function, and every partial it returns, takes any number of arguments. It returns another function while it waits, and
fn's result once it is ready.
Requirements
curry(fn)returns a function and does not callfn.curry.placeholderis a symbol.- Without placeholders it behaves as a plain curry: any grouping of the arguments works, and
fndoes not run early. - New arguments first fill the open positions (placeholders) already collected, from left to right; the ones left over are added at the end.
- A placeholder among the new arguments keeps the position it lands on open: it may fill an old hole with a new hole, or open a new position at the end.
- Placeholders are not arguments:
fnruns only when the list has at leastarityitems and none of the firstarityis a placeholder. fnruns exactly once per completed chain, with every collected argument (extras included) and thethisof the completing call, and that call returns its result.- Every partial can be reused, and partials never affect each other or the curried function.
- Only
curry.placeholderitself is a placeholder:undefined,null, the string"_"and other symbols are ordinary arguments. An array argument stays one argument. - A call with no arguments adds nothing.
arityreplacesfn.lengthwhen given; with an arity of 0, the first call runsfn.
Examples
const _ = curry.placeholder;
const list = curry((a, b, c) => [a, b, c]);
list(_, 2, 3)(1); // [1, 2, 3]
list(_, _, 3)(1)(2); // [1, 2, 3]
list(_, 2, _)(1, 3); // [1, 2, 3]: holes fill from the left
list(1)(_, 3)(2); // [1, 2, 3]const divide = curry((a, b) => a / b);
const halve = divide(_, 2);
halve(10); // 5
halve(9); // 4.5: halve is unchanged by the call beforelist(_, 2)(_, 3)(1); // [1, 2, 3]: the second _ keeps position 0 open, 3 goes at the end
list(_, _, _)(1)(2); // still a function: position 2 is open
list(_, 2, 3, 4)(1); // fn gets (1, 2, 3, 4): extras are passed onEnvironment
- Plain JavaScript in a browser page; the tests call the global
curryand readcurry.placeholderfromcurry.js. Keep the starter's last line, which creates the placeholder. - What
fnreceives when a placeholder sits after the firstaritypositions is not tested. - The version without placeholders is problem 8.2.
Hints
- Each partial holds one list of arguments, which may contain placeholders. What should a new call do to that list, without changing it?
- Is "the list is long enough" the same as "the function is ready"?
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