Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Suggestion: include Complement method for functions #822

Closed
mattdiamond opened this Issue Oct 10, 2012 · 2 comments

Comments

Projects
None yet
3 participants

I'm proposing the addition of a "complement" method that would take a function (and optional context) as arguments and return a function that, when called, will call the original function and return the boolean complement (NOT) of the result.

Here's a basic implementation which should make this clear:

function complement(func, context){
    return function(){
        return !func.apply(context || this, arguments);
    }
}

This could possibly be useful for people that need the complement of a previously stored iterator function (e.g. instantly transforming an isX function to an isNotX function).

This would also allow underscore's _.reject method to easily use the native .filter implementation by passing in the complement of the iterator.

@jashkenas jashkenas closed this in 7419c80 Oct 10, 2012

Owner

jashkenas commented Oct 10, 2012

I don't think that this function is quite common-case enough for core Underscore ... but it's a nice idea to implement reject in terms of select. Thanks.

1800joe commented Mar 20, 2013

This would be incredibly useful, eliminating the need for _.isX === false with such a minimal addition to the code base, big win. I've got an implementation minus context with a few tests. if you're open to reconsidering here it is: https://github.com/1800joe/underscore. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment