Last active
September 12, 2018 20:48
-
-
Save TehShrike/530479cf6397aa819f5ac173a8be0256 to your computer and use it in GitHub Desktop.
chunkWhile
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
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