最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to solve _.some using _.every? - Stack Overflow

programmeradmin0浏览0评论

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. () The part I'm struggling with is it's asking me to find a way to solve it using _.every internally. ()

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?

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/#some) The part I'm struggling with is it's asking me to find a way to solve it using _.every internally. (http://underscorejs/#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?

Share Improve this question edited Aug 10, 2013 at 0:12 Paul Phillips 6,25927 silver badges35 bronze badges asked Aug 10, 2013 at 0:04 jjjjjj 1231 silver badge6 bronze badges 2
  • 1 You got the right idea. However, you need to pose the ! operator and the function; it's something like function(x){return !truthStatementFunction(x)} instead of just !truthStatementFunction. – user824425 Commented Aug 10, 2013 at 0:08
  • 1 To answer your question... It's a learning exercise so the point is to learn, one way or another (although I know solving it 100% on my own would be the most effective way to learn, getting stuck too long and wasting time that I could be spending learning other equally useful things does me no good). I'd prefer to get a hint here rather than an outright answer, but I wasn't sure if that would run afoul of stack overflow's seemingly stringent rules. – jjj Commented Aug 10, 2013 at 2:02
Add a ment  | 

1 Answer 1

Reset to default 9

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;
}
发布评论

评论列表(0)

  1. 暂无评论