Last active
August 1, 2018 19:21
-
-
Save laurentjacob/db1df73d5a3e8e66b749d70f66a11f1e to your computer and use it in GitHub Desktop.
Code for partialApply blog post
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
| function add (x, y, z) { | |
| return x + y + z; | |
| } |
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
| partialApply(add, 1)(1)(2); // yields 4 | |
| partialApply(add)(1)(1)(2); // yields 4 | |
| partialApply(add)(1, 1)(2); // yields 4 | |
| partialApply(add, 1, 1, 2); // yields 4 |
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
| add.partialApply(1)(1)(2); // yields 4 | |
| add.partialApply(1, 1)(2); // yields 4 | |
| add.partialApply(1, 1, 2); // yields 4 |
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
| Function.prototype.partialApply = function() { | |
| var fn = this; | |
| var slice = Array.prototype.slice; | |
| var curr_args = slice.call(arguments); | |
| if(fn.length > curr_args.length) { | |
| return function () { | |
| var new_args = slice.call(arguments); | |
| var args = curr_args.concat(new_args); | |
| return fn.partialApply.apply(fn, args); | |
| } | |
| } | |
| else { | |
| return fn.apply(null, curr_args); | |
| } | |
| }; |
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
| return fn.partialApply.apply(fn, args); |
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
| return fn.partialApply.apply(null, args); |
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
| var add = function(x, y, z) { | |
| return x + y + z; | |
| }.partialApply(); | |
| var a = add(1, 2, 3); // yields 6 | |
| var b = add(1)(2)(3); // yields 6 | |
| var c = add(1)(2, 3); // yields 6 |
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
| var partialAdd = add(1); // yields a function that needs 2 more arguments | |
| var result = partialAdd(2, 3); // yields 6 |
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
| add(1, 2, 3); // yields 6 | |
| add(1)(2)(3); // yields 6 |
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
| function schonfinkelize(fn) { | |
| var slice = Array.prototype.slice, | |
| stored_args = slice.call(arguments, 1); | |
| return function () { | |
| var new_args = slice.call(arguments), | |
| args = stored_args.concat(new_args); | |
| return fn.apply(null, args); | |
| }; | |
| } |
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
| var addSecondHalf = schonfinkelize(add, 1); // yields a function | |
| addSecondHalf(2, 3); // yields 6 |
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
| schonfinkelize(add, 1)(2)(3); // Doesn't work |
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
| schonfinkelize(schonfinkelize(add, 1)(2))(3); // yields 6 |
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
| add.length // yields 3 |
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
| function partialApply(fn) { | |
| var slice = Array.prototype.slice; | |
| var curr_args = slice.call(arguments, 1); | |
| if(fn.length > curr_args.length) { | |
| return function() { | |
| var new_args = slice.call(arguments); | |
| var args = curr_args.concat(new_args); | |
| return partialApply.apply(null, [fn].concat(args)); | |
| } | |
| } | |
| else { | |
| return fn.apply(null, curr_args); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment