Skip to content

Instantly share code, notes, and snippets.

@TehShrike
Last active September 12, 2018 20:48
Show Gist options
  • Save TehShrike/530479cf6397aa819f5ac173a8be0256 to your computer and use it in GitHub Desktop.
Save TehShrike/530479cf6397aa819f5ac173a8be0256 to your computer and use it in GitHub Desktop.
chunkWhile
const chunkWhile = (iterable, condition) => [...chunkGenerator(iterable, condition)]
function* chunkGenerator(iterable, condition) {
let currentChunk = []
for (const element of iterable) {
currentChunk.push(element)
if (!condition(element)) {
yield currentChunk
currentChunk = []
}
}
if (currentChunk.length) {
yield currentChunk
}
}
console.log(
chunkWhile([1,2,1,2,2,1,1,2], n => n !== 1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment