Skip to content

Instantly share code, notes, and snippets.

@krainboltgreene
Last active August 9, 2019 02:10
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 krainboltgreene/518452ef2b5278d1da9bc04753e3303c to your computer and use it in GitHub Desktop.
Save krainboltgreene/518452ef2b5278d1da9bc04753e3303c to your computer and use it in GitHub Desktop.
const chunk =
(maximum) =>
(list) =>
reduceWithValueKey(
(accumulated) =>
(item) =>
(index) => {
const [latestChunk, ...previousChunks] = accumulated;
if ((length(latestChunk) !== maximum) && (length(list) === index + 1)) {
return reverse([[...latestChunk, item], ...previousChunks]);
}
if (length(list) === index + 1) {
return reverse([[item], ...accumulated]);
}
if (length(latestChunk) !== maximum) {
return [[...latestChunk, item], ...previousChunks]
}
return [[item], ...accumulated];
}
)(
[[]]
)(
list
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment