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
- 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.
- As soon as every non-placeholder parameter has been supplied, and all placeholders have been filled, invoke the original function.
- Support supplying one or more arguments per call, mixing real values and placeholders.
- 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: