I am trying to understand partial functions. I found this example (http://blog.thesoftwarecraft.com/2013/05/partial-functions-in-javascript.html) and I am unable to understand completely.
function partial(f) {
console.log(f) // tip(p,check)
var args = Array.prototype.slice.call(arguments, 1); //0.2
var test_args = Array.prototype.slice.call(arguments);
console.warn(test_args) // [tip(p,check), 0.2]
return function () {
console.warn(arguments) //[120, 0, [120, 90, 180]] [90, 0, [120, 90, 180]] ...
//where do these arguments come from? why don't appear at test_args?
var other_args = Array.prototype.slice.call(arguments); //[120, 0, [120, 90, 180]] [90, 0, [120, 90, 180]] ...
console.log(args.concat(other_args)) // added percentage to array[0.2, 120, 0, [120, 90, 180]]
return f.apply(null, args.concat(other_args)); //we execute tip with all the arguments (only 2 first will be used)
}
}
function tip(percentage, check) {
return check * percentage
}
[120, 90, 180].map(partial(tip, 0.2)); //[24, 18, 36]