0

I have many arrays of functions of variable length in which each function in an array takes a single argument of the same type and returns true or false.

How can I "compose" these functions together into a single function that tests that each and every component function returns true?

var less_than = function(y) {
    function(x) {
    return x < y;
  }
}

var greater_than = function(y) {
    function(x) {
    return x > y;
  }
}

var is_even = function(x) {
    return x % 2 == 0;
}

var fns = [less_than(10), greater_than(1), is_even];

var test_function = combine_tests(fns);
test_function(8) // => true
0

1 Answer 1

5

The every() method tests whether all elements in the array pass the test implemented by the provided function.

var functions = [f1, f2, f3];
var arg = "foo";
var result = functions.every(function(func){
    return func(arg);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.