Skip to content

Instantly share code, notes, and snippets.

@viebel
Last active September 20, 2016 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/d5074a79db2bdb65f5e94627b901ba86 to your computer and use it in GitHub Desktop.
Save viebel/d5074a79db2bdb65f5e94627b901ba86 to your computer and use it in GitHub Desktop.
// Thank you http://stackoverflow.com/a/32234086/813665
var extremumBy = function(pluck, extremum, arr) {
return arr.reduce(function(best, next) {
var pair = [pluck(next), next];
if (best === null) {
return pair;
}
if (extremum(best[0], pair[0]) === best[0]) {
return best;
}
return pair;
}, null)[1];
};
minBy = function(fn, arr) {
return extremumBy(fn, Math.min, arr);
};
maxBy = function(fn, arr) {
return extremumBy(fn, Math.max, arr);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment