Skip to content

Commit 0fa1071

Browse files
committed
Merge pull request #2137 from megawac/partion-group
Implement partion via group
2 parents e4743ab + c545b48 commit 0fa1071

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

underscore.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@
391391
};
392392

393393
// An internal function used for aggregate "group by" operations.
394-
var group = function(behavior) {
394+
var group = function(behavior, partition) {
395395
return function(obj, iteratee, context) {
396-
var result = {};
396+
var result = partition ? [[], []] : {};
397397
iteratee = cb(iteratee, context);
398398
_.each(obj, function(value, index) {
399399
var key = iteratee(value, index, obj);
@@ -438,14 +438,9 @@
438438

439439
// Split a collection into two arrays: one whose elements all satisfy the given
440440
// predicate, and one whose elements all do not satisfy the predicate.
441-
_.partition = function(obj, predicate, context) {
442-
predicate = cb(predicate, context);
443-
var pass = [], fail = [];
444-
_.each(obj, function(value, key, obj) {
445-
(predicate(value, key, obj) ? pass : fail).push(value);
446-
});
447-
return [pass, fail];
448-
};
441+
_.partition = group(function(result, value, pass) {
442+
result[pass ? 0 : 1].push(value);
443+
}, true);
449444

450445
// Array Functions
451446
// ---------------

0 commit comments

Comments
 (0)