Created
December 21, 2016 18:09
-
-
Save tonyonodi/85b05497272cdc979657223389838077 to your computer and use it in GitHub Desktop.
Functional batching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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