The function "filter" returns an array [0,4]
but I don't understand how it gets that. Can you explain "partial"? Is it a built in function? I'm assuming that "op" applies the ">" operator to the numbers in 开发者_如何学编程the array. So since 5 is greater than 0 it gets added to the array "result". But how does "partial" work?
function filter(test, array) {
var result = [];
forEach(array, function (element) {
if (test(element))
result.push(element);
});
return result;
}
show(filter(partial(op[">"], 5), [0, 4, 8, 12]));
In this case partial takes a function of 2 inputs and one value. Call them f(x,y) and a. It returns a function of one input g(z). When the you call g(b) it returns f(a,b). Thus its a partial application. Filter need functions of one input, while '<' is a 2 input function.
Partial is a function that takes a function and returns a function, that preassignes one (or more) of the inputs.
精彩评论