Last active
August 29, 2015 13:58
-
-
Save isao/10423591 to your computer and use it in GitHub Desktop.
call a function with a subset (slice) of the arguments passed to it.
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 callSliced(fn, a, b) { | |
var args = [a]; // 1st argument to slice | |
if (b) args.push(b); // 2nd argument to slice, if any | |
return function apply() { | |
return fn.apply(fn, Array.prototype.slice.apply(arguments, 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
// the function created by `callSliced(console.log, 0, 1)` | |
// only uses the first argument passed to it | |
> [1,2,3].forEach(callSliced(console.log, 0, 1)); | |
1 | |
2 | |
3 | |
> [1,2,3].forEach(console.log) | |
1 0 [ 1, 2, 3 ] | |
2 1 [ 1, 2, 3 ] | |
3 2 [ 1, 2, 3 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment