From c8e3c04076c2e5310c31db0810977f517a39ee6b Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Tue, 5 Apr 2011 18:09:47 -0400 Subject: [PATCH 1/3] Adding _.count to count truthy values in an iterator. _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }) = 3 --- index.html | 23 +++++++++++++++++------ test/collections.js | 7 +++++++ underscore.js | 10 ++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 215d06a6a..a7d9c5284 100644 --- a/index.html +++ b/index.html @@ -137,12 +137,13 @@

Table of Contents

each, map, reduce, reduceRight, detect, select, - reject, all, - any, include, - invoke, pluck, - max, min, - sortBy, sortedIndex, - toArray, size + reject, count, + all, any, + include, invoke, + pluck, max, + min, sortBy, + sortedIndex, toArray, + size

@@ -337,6 +338,16 @@

Collection Functions (Arrays or Objects)

 var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
 => [1, 3, 5]
+
+ +

+ count_.count(list, [iterator], [context]) +
+ Returns a count of elements which pass the truth test (iterator). +

+
+var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
+=> 3
 

diff --git a/test/collections.js b/test/collections.js index 442b205f1..b14821a9d 100644 --- a/test/collections.js +++ b/test/collections.js @@ -122,6 +122,13 @@ $(document).ready(function() { equals(odds.join(', '), '1, 3, 5', 'rejected each even number'); }); + test('collections: count', function() { + var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); + equals(count, 3, 'counted even numbers'); + var count = _.count([0, 1, 2, 3, 4, 5, 6]); + equals(count, 6, 'counted all truthy numbers'); + }); + test('collections: all', function() { ok(_.all([]), 'the empty set'); ok(_.all([true, true, true]), 'all true values'); diff --git a/underscore.js b/underscore.js index 259a4f310..313b16c1e 100644 --- a/underscore.js +++ b/underscore.js @@ -164,6 +164,16 @@ return results; }; + // Returns a count of elements which pass a truth test. + _.count = function(obj, iterator, context) { + var count = 0; + iterator = iterator || _.identity; + each(obj, function(value, index, list) { + if (iterator.call(context, value, index, list)) count += 1; + }); + return count; + }; + // Determine whether all of the elements match a truth test. // Delegates to **ECMAScript 5**'s native `every` if available. // Aliased as `all`. From 54575225822e7b68926d68b84b832c65690f76a4 Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Tue, 5 Apr 2011 18:11:20 -0400 Subject: [PATCH 2/3] Speeding up a few methods which assign a default identity function for missing/optional iterators. Noticeable difference. --- underscore.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/underscore.js b/underscore.js index 313b16c1e..1630e213b 100644 --- a/underscore.js +++ b/underscore.js @@ -167,7 +167,7 @@ // Returns a count of elements which pass a truth test. _.count = function(obj, iterator, context) { var count = 0; - iterator = iterator || _.identity; + iterator || (iterator = _.identity); each(obj, function(value, index, list) { if (iterator.call(context, value, index, list)) count += 1; }); @@ -178,7 +178,7 @@ // Delegates to **ECMAScript 5**'s native `every` if available. // Aliased as `all`. _.every = _.all = function(obj, iterator, context) { - iterator = iterator || _.identity; + iterator || (iterator = _.identity); var result = true; if (obj == null) return result; if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context); @@ -192,7 +192,7 @@ // Delegates to **ECMAScript 5**'s native `some` if available. // Aliased as `any`. var any = _.some = _.any = function(obj, iterator, context) { - iterator = iterator || _.identity; + iterator || (iterator = _.identity); var result = false; if (obj == null) return result; if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); @@ -265,7 +265,7 @@ // Use a comparator function to figure out at what index an object should // be inserted so as to maintain order. Uses binary search. _.sortedIndex = function(array, obj, iterator) { - iterator = iterator || _.identity; + iterator || (iterator = _.identity); var low = 0, high = array.length; while (low < high) { var mid = (low + high) >> 1; @@ -439,7 +439,7 @@ // Memoize an expensive function by storing its results. _.memoize = function(func, hasher) { var memo = {}; - hasher = hasher || _.identity; + hasher || (hasher = _.identity); return function() { var key = hasher.apply(this, arguments); return hasOwnProperty.call(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments)); From 1fc7d4b04982b7afa221e3f5b2c81fc5547353a5 Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Wed, 6 Apr 2011 09:03:40 -0400 Subject: [PATCH 3/3] Revert "Adding _.count to count truthy values in an iterator. _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }) = 3" This reverts commit c8e3c04076c2e5310c31db0810977f517a39ee6b. Conflicts: underscore.js --- index.html | 23 ++++++----------------- test/collections.js | 7 ------- underscore.js | 10 ---------- 3 files changed, 6 insertions(+), 34 deletions(-) diff --git a/index.html b/index.html index a7d9c5284..215d06a6a 100644 --- a/index.html +++ b/index.html @@ -137,13 +137,12 @@

Table of Contents

each, map, reduce, reduceRight, detect, select, - reject, count, - all, any, - include, invoke, - pluck, max, - min, sortBy, - sortedIndex, toArray, - size + reject, all, + any, include, + invoke, pluck, + max, min, + sortBy, sortedIndex, + toArray, size

@@ -338,16 +337,6 @@

Collection Functions (Arrays or Objects)

 var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
 => [1, 3, 5]
-
- -

- count_.count(list, [iterator], [context]) -
- Returns a count of elements which pass the truth test (iterator). -

-
-var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
-=> 3
 

diff --git a/test/collections.js b/test/collections.js index b14821a9d..442b205f1 100644 --- a/test/collections.js +++ b/test/collections.js @@ -122,13 +122,6 @@ $(document).ready(function() { equals(odds.join(', '), '1, 3, 5', 'rejected each even number'); }); - test('collections: count', function() { - var count = _.count([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); - equals(count, 3, 'counted even numbers'); - var count = _.count([0, 1, 2, 3, 4, 5, 6]); - equals(count, 6, 'counted all truthy numbers'); - }); - test('collections: all', function() { ok(_.all([]), 'the empty set'); ok(_.all([true, true, true]), 'all true values'); diff --git a/underscore.js b/underscore.js index 1630e213b..e142ec90c 100644 --- a/underscore.js +++ b/underscore.js @@ -164,16 +164,6 @@ return results; }; - // Returns a count of elements which pass a truth test. - _.count = function(obj, iterator, context) { - var count = 0; - iterator || (iterator = _.identity); - each(obj, function(value, index, list) { - if (iterator.call(context, value, index, list)) count += 1; - }); - return count; - }; - // Determine whether all of the elements match a truth test. // Delegates to **ECMAScript 5**'s native `every` if available. // Aliased as `all`.