Permalink
Browse files

Fix reduceMin and reduceMax.

Javascript's sort() function sorts lexically and thus will not sort
numbers correctly without a comparator function. Reduce/fold is the
proper way to find a minimum or maximum.
  • Loading branch information...
1 parent e7d09e8 commit 311a30faa91d8dcaabef0d21fda9db4c85046ad0 @seancribbs seancribbs committed with kellymclaughlin Jan 17, 2011
Showing with 12 additions and 4 deletions.
  1. +12 −4 priv/mapred_builtins.js
@@ -72,12 +72,20 @@ var Riak = function() {
return [0];
}},
reduceMin: function(values, arg) {
- values.sort();
- return [values[0]];
+ if(values.length == 0)
+ return [];
+ else
+ return [values.reduce(function(prev,next){
+ return (prev < next) ? prev : next;
+ })];
},
reduceMax: function(values, arg) {
- values.sort().reverse();
- return [values[0]];
+ if(values.length == 0)
+ return [];
+ else
+ return [values.reduce(function(prev,next){
+ return (prev > next) ? prev : next;
+ })];
},
reduceSort: function(value, arg) {
try {

0 comments on commit 311a30f

Please sign in to comment.