Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created June 25, 2019 08:06
Show Gist options
  • Save tail-call/f8569028801c7dcfe22d6b1cbb72d664 to your computer and use it in GitHub Desktop.
Save tail-call/f8569028801c7dcfe22d6b1cbb72d664 to your computer and use it in GitHub Desktop.
Functions with verbose call syntax
import verboseFunction from './verbose.js';
const execute = verboseFunction(["after", "function"], (ms, cb) => setTimeout(cb, ms));
execute.after(1000).function(() => console.log("so verbose!"));
// You also get currying for free
const waitOneSecondAndExecute = execute.after(1000);
waitOneSecondAndExecute.function(() => alert("see what I mean?"));
export default function verboseFunction(params, func, values = []) {
if (!params.length) return func.apply(this, values);
return {
[params[0]]: function(value) {
return verboseFunction(params.slice(1), func, [...values, value])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment