Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active February 26, 2020 06:08
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 leodutra/1eebb61d53ff8002ea91f7222d6e3003 to your computer and use it in GitHub Desktop.
Save leodutra/1eebb61d53ff8002ea91f7222d6e3003 to your computer and use it in GitHub Desktop.
Reducer library for JS
const sumReducer = (sum, x) => sum + x;
const minReducer = (min/* =null */, x) => {
if (min == null) return x;
return min < x ? min : x;
};
const maxReducer = (max/* =null */, x) => {
if (max == null) return x;
return max > x ? max : x;
};
const meanReducer = (total, amount, index, array) =>
(index === array.length - 1
? (total + amount) / array.length
: total + amount);
const ascendingSort = (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
const descendingSort = (a, b) => {
if (a < b) return 1;
if (a > b) return -1;
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment