Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Created December 21, 2016 18:09
Show Gist options
  • Save tonyonodi/85b05497272cdc979657223389838077 to your computer and use it in GitHub Desktop.
Save tonyonodi/85b05497272cdc979657223389838077 to your computer and use it in GitHub Desktop.
Functional batching
// batch([1,2,3,4,5,6,7,8,9,10], 5) -> [[1,2,3,4,5], [6,7,8,9,10]]
const batch = (collection, batchSize) => {
return collection.reduce((accumulator, value, index) => {
const newAccumulator = index % batchSize === 0 ?
[...accumulator, []] :
accumulator;
const init = newAccumulator.slice(0, -1);
const last = newAccumulator.slice(-1)[0];
return [...init, [...last, value]];
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment