Last active
March 2, 2023 15:08
-
-
Save airled/b8ca045171b136de16f939461f9d90b1 to your computer and use it in GitHub Desktop.
Simple currying example in JS
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
| class Curry { | |
| constructor(fn) { | |
| return this.curry(fn, fn.length, []) | |
| } | |
| curry(fn, argRestCounter, args) { | |
| if (argRestCounter === 0) return fn(…args); | |
| return arg => this.curry(fn, argRestCounter — 1, […args, arg]); | |
| } | |
| } | |
| const f = (x, y, z) => x * y + z; | |
| const cur = new Curry(f); | |
| cur(2)(3)(4) // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment