Skip to content

Commit

Permalink
Fix reduceMin and reduceMax.
Browse files Browse the repository at this point in the history
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
seancribbs authored and kellymclaughlin committed May 31, 2011
1 parent e7d09e8 commit 311a30f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions priv/mapred_builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 311a30f

Please sign in to comment.