Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Quantifiers, exactly and only #518

Closed
wants to merge 6 commits into from

3 participants

@joshdk

Currently, underscore implements two different quantifiers;

  • The universal quantifier _.all, "do all elements pass a truth test?".
  • The existential quantifier _.any, "do any elements pass a truth test?".

This for aims to add two more quantifiers;

  • An exact quantifier _.exactly, "do exactly n elements pass a truth test?".
  • The uniqueness quantifier ∃! _.only, "does only one element pass a truth test?". This is a specific case of exactly, where n is 1.
joshdk added some commits
@joshdk joshdk Added an _.exactly quantifier
- Similar to _.any and _.all, _.exactly returns true if an exact number of elements pass a truth test.
c75dee8
@joshdk joshdk Covered condition when obj was null
- If obj is null, and count is 0, yield true.
b44aacb
@joshdk joshdk Added testing functions for _.exactly 662e110
@joshdk joshdk Merge remote-tracking branch 'upstream/master' into quantifier-exactly 003c53c
@joshdk joshdk Added an _.only quantifier
- Used for determining of only 1 element passes a truth test.
5e777ad
@joshdk joshdk Added tests for _.only 6c6da43
@jashkenas
Owner

The goal of Underscore isn't to provide every logical predicate under the sun, but rather the useful ones.

Got any real-world code handy with use cases for when you've needed these?

@jashkenas
Owner

Looks like that's a "no".

@jashkenas jashkenas closed this
@arbales

(But, @joshdk — since you wrote them and you'd use them, they'd probably make a great underscore.predicates.js)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 17, 2012
  1. @joshdk

    Added an _.exactly quantifier

    joshdk authored
    - Similar to _.any and _.all, _.exactly returns true if an exact number of elements pass a truth test.
  2. @joshdk

    Covered condition when obj was null

    joshdk authored
    - If obj is null, and count is 0, yield true.
  3. @joshdk
Commits on Mar 21, 2012
  1. @joshdk
  2. @joshdk

    Added an _.only quantifier

    joshdk authored
    - Used for determining of only 1 element passes a truth test.
  3. @joshdk

    Added tests for _.only

    joshdk authored
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 0 deletions.
  1. +24 −0 test/collections.js
  2. +22 −0 underscore.js
View
24 test/collections.js
@@ -166,6 +166,30 @@ $(document).ready(function() {
Array.prototype.some = nativeSome;
});
+ test('collections: exactly', function() {
+ var test=[0,1,2,3,4,5,6,7,8,9];
+ ok(_.exactly([],_.identity,0), 'the empty set');
+ ok(_.exactly(test, function(x){ return false; },0), 'exactly 0');
+ ok(_.exactly(test, function(x){ return x==5; },1), 'exactly 1');
+ ok(_.exactly(test, function(x){ return true; },test.length), 'exactly all');
+ ok(!_.exactly(test, function(num){ return num<=5; }, 8), 'false when too few');
+ ok(!_.exactly(test, function(num){ return num<=5; }, 2), 'false when too many');
+ ok(!_.exactly(test, function(num){ return num<=5; }, -1), 'false when count is negative');
+ ok(_.exactly(null, function(num){ return num<=5; }, 0), 'obj is null and count is 0');
+ ok(!_.exactly(null, function(num){ return num<=5; }, 5), 'obj is null and count is non-0');
+ });
+
+ test('collections: only', function() {
+ var test=[0,1,2,3,4,5,6,7,8,9];
+ ok(!_.only([],_.identity), 'the empty set');
+ ok(!_.only(test, function(x){ return false; }), '0 match');
+ ok(_.only(test, function(x){ return x==5; }), '1 match');
+ ok(!_.only(test, function(x){ return true; }), 'many match');
+ ok(!_.only([], function(x){ return true; }), 'obj is empty');
+ ok(!_.only(null, function(x){ return true; }), 'obj is null');
+ ok(_.one(test, function(x){ return x==5; }), 'aliased as "one"');
+ });
+
test('collections: include', function() {
ok(_.include([1,2,3], 2), 'two is in the array');
ok(!_.include([1,3,9], 2), 'two is not in the array');
View
22 underscore.js
@@ -197,6 +197,28 @@
return !!result;
};
+ // Determine if exactly count elements in the object matches a truth test.
+ _.exactly = function(obj, iterator, count, context){
+ var result = true;
+ if (obj == null) return count == 0;
+ each(obj, function(value, index, list) {
+ if (iterator.call(context, value, index, list)){
+ if (count == 0){
+ result = false;
+ return breaker;
+ }
+ count--;
+ }
+ });
+ return result && count == 0;
+ }
+
+ // Determine if only one element in the object matches a truth test.
+ // Aliased as `one`
+ _.one = _.only = function(obj, iterator, context){
+ return _.exactly(obj,iterator,1,context);
+ }
+
// Determine if a given value is included in the array or object using `===`.
// Aliased as `contains`.
_.include = _.contains = function(obj, target) {
Something went wrong with that request. Please try again.