Created
June 25, 2019 08:06
-
-
Save tail-call/f8569028801c7dcfe22d6b1cbb72d664 to your computer and use it in GitHub Desktop.
Functions with verbose call syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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?")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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