Last active
August 29, 2015 14:07
-
-
Save getify/b1435aa3b419f89d1f9c to your computer and use it in GitHub Desktop.
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 spread(fn) { | |
| return function(args){ | |
| fn.apply(null,args); | |
| }; | |
| } | |
| // ****************** | |
| function foo(x,y) { | |
| console.log(x,y); | |
| } | |
| spread(foo)([10,20]); // 10 20 | |
| // Trying to find the Functional equivalent to `spread(..)` | |
| // Something like: | |
| Function.apply.bind(foo)([10,20]); // undefined undefined | |
| // see, that doesn't quite work. help? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function.bind() allows to execute .apply() in the context of the spread / plus function (pretty much the same as
plus.apply()).The first argument of .apply() defines the context .apply() should execute in, the second are the actual parameters as an array (
fn.apply(context, args).So this should also work:
Function.apply.bind(plus)(null, [1,2])But that is not as nice.
Function.bind() also allows partial application with predefined arguments:
They are using this to partially apply the null value as the context for .apply()