#455 Curry with Placeholder

medium
javascript

This is a follow-up to the basic currying problem. In addition to regular currying, the new curry helper must support placeholders that allow arguments to be supplied out of order.

Requirements

  1. Expose a static curry.placeholder symbol (or unique value). Users pass this placeholder in any position to mark an argument as to be provided later.
  2. As soon as every non-placeholder parameter has been supplied, and all placeholders have been filled, invoke the original function.
  3. Support supplying one or more arguments per call, mixing real values and placeholders.
  4. Preserve behavior of the previous curry implementation: collect arguments until arity is satisfied.

Signature

const _: unique symbol;          // exported via curry.placeholder
function curry<F extends (...args: any[]) => any>(fn: F): Curried<F>;

Examples

const join = (a: any, b: any, c: any) => `${a}_${b}_${c}`;
const curriedJoin = curry(join);
const _ = curry.placeholder;

curriedJoin(1, 2, 3);           // '1_2_3'
curriedJoin(_, 2)(1, 3);        // '1_2_3'
curriedJoin(_, _, _)(1)(_, 3)(2); // '1_2_3'

Further reading:

  • https://javascript.info/currying-partials
  • https://lodash.com/docs/4.17.15#curry
  • https://github.com/planttheidea/curriable