|
31 | 31 | }; |
32 | 32 | }; |
33 | 33 |
|
| 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 | + |
34 | 48 | // n.b. depends on underscore.function.arity.js |
| 49 | + // n.b. depends on underscore.array.builders.js |
35 | 50 |
|
36 | 51 | // Takes a target function and a mapping function. Returns a function |
37 | 52 | // that applies the mapper to its arguments before evaluating the body. |
|
64 | 79 | // Composes a bunch of predicates into a single predicate that |
65 | 80 | // checks all elements of an array for conformance to all of the |
66 | 81 | // 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')), |
78 | 83 |
|
79 | 84 | // Composes a bunch of predicates into a single predicate that |
80 | 85 | // checks all elements of an array for conformance to any of the |
81 | 86 | // 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'), |
93 | 88 |
|
94 | 89 | // Takes a predicate-like and returns a comparator (-1,0,1). |
95 | 90 | comparator: function(fun) { |
|
0 commit comments