Last active
August 29, 2015 13:58
-
-
Save isao/10424289 to your computer and use it in GitHub Desktop.
partial application of parameters, allowing (based on <http://ejohn.org/blog/partial-functions-in-javascript/>)
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
// fn - function to call | |
// signature - array of arguments to use to call fn, if an array value is `undefined` | |
// then use the curried/partial function parameter | |
function partial(fn, signature) { | |
var slice = Array.prototype.slice; | |
return function apply() { | |
var args = slice.call(arguments); | |
return fn.apply(fn, signature.map(function fillIn(arg) { | |
return arg === undefined ? args.shift() : arg; | |
})); | |
}; | |
} |
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 log = console.log.bind(console) | |
> var argLimit = partial(log, [undefined, undefined]); | |
> argLimit(1, 2, 3, 4); | |
1 2 | |
> var argMash1 = partial(log, [1, undefined, 3, undefined]); | |
> argMash1(2, 4); | |
1 2 3 4 | |
> var argMash2 = partial(log, ['the array item value is', undefined, 'the array item index is', undefined]); | |
> [11, 22, 33].forEach(argMash2); | |
the array item value is 11 the array item index is 0 | |
the array item value is 22 the array item index is 1 | |
the array item value is 33 the array item index is 2 | |
> var myLog = partial(log, ['isao', undefined, '=>', undefined]) | |
> myLog(1,2,3) | |
isao 1 => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment