Skip to content

Commit 1492ffb

Browse files
committed
Merge pull request documentcloud#163 from jacobpurcell/conjoin-disjoin-single-elem
Support conjoin and disjoin methods for single elements
2 parents b8b45d1 + ee34a5f commit 1492ffb

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

test/function.combinators.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ $(document).ready(function() {
1818
test("conjoin", function() {
1919
var isPositiveEven = _.conjoin(function(x) { return x > 0; }, function(x) { return (x & 1) === 0; });
2020

21+
equal(isPositiveEven(2), true, 'should recognize that element satisfies a conjunction');
22+
equal(isPositiveEven(1), false, 'should recognize that element does not satisfy a conjunction');
2123
equal(isPositiveEven([2,4,6,8]), true, 'should recognize when all elements satisfy a conjunction');
2224
equal(isPositiveEven([2,4,6,7,8]), false, 'should recognize when an element fails to satisfy a conjunction');
2325
});
2426

2527
test("disjoin", function() {
2628
var orPositiveEven = _.disjoin(function(x) { return x > 0; }, function(x) { return (x & 1) === 0; });
2729

30+
equal(orPositiveEven(2), true, 'should recognize that element satisfies a disjunction');
31+
equal(orPositiveEven(-1), false, 'should recognize that element does not satisfy a disjunction');
2832
equal(orPositiveEven([-1,2,3,4,5,6]), true, 'should recognize when all elements satisfy a disjunction');
2933
equal(orPositiveEven([-1,-3]), false, 'should recognize when an element fails to satisfy a disjunction');
3034
});

underscore.function.combinators.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,22 @@
3131
};
3232
};
3333

34+
var createPredicateApplicator = function (funcToInvoke /*, preds */) {
35+
var preds = _(arguments).tail();
36+
37+
return function (objToCheck) {
38+
var array = _(objToCheck).cat();
39+
40+
return _[funcToInvoke](array, function (e) {
41+
return _[funcToInvoke](preds, function (p) {
42+
return p(e);
43+
});
44+
});
45+
};
46+
};
47+
3448
// n.b. depends on underscore.function.arity.js
49+
// n.b. depends on underscore.array.builders.js
3550

3651
// Takes a target function and a mapping function. Returns a function
3752
// that applies the mapper to its arguments before evaluating the body.
@@ -64,32 +79,12 @@
6479
// Composes a bunch of predicates into a single predicate that
6580
// checks all elements of an array for conformance to all of the
6681
// original predicates.
67-
conjoin: function(/* preds */) {
68-
var preds = arguments;
69-
70-
return function(array) {
71-
return _.every(array, function(e) {
72-
return _.every(preds, function(p) {
73-
return p(e);
74-
});
75-
});
76-
};
77-
},
82+
conjoin: _.partial(createPredicateApplicator, ('every')),
7883

7984
// Composes a bunch of predicates into a single predicate that
8085
// checks all elements of an array for conformance to any of the
8186
// original predicates.
82-
disjoin: function(/* preds */) {
83-
var preds = arguments;
84-
85-
return function(array) {
86-
return _.some(array, function(e) {
87-
return _.some(preds, function(p) {
88-
return p(e);
89-
});
90-
});
91-
};
92-
},
87+
disjoin: _.partial(createPredicateApplicator, 'some'),
9388

9489
// Takes a predicate-like and returns a comparator (-1,0,1).
9590
comparator: function(fun) {

0 commit comments

Comments
 (0)