Skip to curriculum
Jump to the editor

8.3. Curry with placeholders

Hard
Topics

This question is in

8. Currying and memoization
30–45 min

A 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

JAVASCRIPT
function curry(fn, arity) // returns the curried function
curry.placeholder         // a Symbol, defined at the bottom of curry.js
  • fn: the function to curry.
  • arity (optional): how many positions must be filled before fn runs. When it is missing, use fn.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

  1. curry(fn) returns a function and does not call fn. curry.placeholder is a symbol.
  2. Without placeholders it behaves as a plain curry: any grouping of the arguments works, and fn does not run early.
  3. New arguments first fill the open positions (placeholders) already collected, from left to right; the ones left over are added at the end.
  4. 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.
  5. Placeholders are not arguments: fn runs only when the list has at least arity items and none of the first arity is a placeholder.
  6. fn runs exactly once per completed chain, with every collected argument (extras included) and the this of the completing call, and that call returns its result.
  7. Every partial can be reused, and partials never affect each other or the curried function.
  8. Only curry.placeholder itself is a placeholder: undefined, null, the string "_" and other symbols are ordinary arguments. An array argument stays one argument.
  9. A call with no arguments adds nothing. arity replaces fn.length when given; with an arity of 0, the first call runs fn.

Examples

JAVASCRIPT
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]
JAVASCRIPT
const divide = curry((a, b) => a / b);
const halve = divide(_, 2);
halve(10);           // 5
halve(9);            // 4.5: halve is unchanged by the call before
JAVASCRIPT
list(_, 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 on

Environment

  • Plain JavaScript in a browser page; the tests call the global curry and read curry.placeholder from curry.js. Keep the starter's last line, which creates the placeholder.
  • What fn receives when a placeholder sits after the first arity positions is not tested.
  • The version without placeholders is problem 8.2.

Hints

  1. Each partial holds one list of arguments, which may contain placeholders. What should a new call do to that list, without changing it?
  2. 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
Back to the question
Loading the editor…