How to solve _.some using _.every?

2019-04-09 01:30发布

问题:

Working on a programming challenge to re-implement the functionality of underscore.js in standard javascript. Specifically I am working on implementing the _.some function. (http://underscorejs.org/#some) The part I'm struggling with is it's asking me to find a way to solve it using _.every internally. (http://underscorejs.org/#every)

I have already finished the _.every function earlier and it works as it should.

Here is logically what I am wanting to do in sketched code:

_.some = function(collection, truthStatementFunction) { 
  return !(_every(collection, !truthStatementFunction)) 
}

Or in english, flip the truth statement to test where the condition is false... and if the _.every test then returns true... then we know that _.some of the original truth statement is false (so flip the return of _.every to get the correct return for _some). Likewise if _.every returns false then flip that to get the correct return of true for _.some.

Obviously the problem with this sketch is the !truthStatementFunction part. How do I get inside that iterator to change the internals of the function to flip it? Doesn't seem that the internals of the function are accessible...

Am I barking up the wrong tree entirely, and there is a better way to solve this using _.every?

回答1:

Pass _every a function that returns an inversion of the result from truthStatementFunction:

_.some = function(collection, truthStatementFunction) { 
    return !(_every(collection, function(v) {
        return !truthStatementFunction(v);
    }));
}

To answer the second part of your question:

and is there a better way to solve this using _.every?

There is definitely a better way to solve this than using _.every. Just iterate over the collection, and return true as soon as you find any element that matches. Much more efficient in cases where _.some is going to return true, since you don't really care how many elements in the collection satisfy the predicate, as long as there is at least one.

_.some = function(c, pred) { 
    for(var i = 0; i < c.length; i++) {
        if (pred(c[i])) return true;
    }
    return false;
}