Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript filter partial op

Tags:

javascript

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]));
like image 584
mjmitche Avatar asked May 23 '26 12:05

mjmitche


1 Answers

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.

like image 156
Rado Avatar answered May 26 '26 00:05

Rado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!