Skip to content

Instantly share code, notes, and snippets.

@lffg
Created December 31, 2021 18:46
Show Gist options
  • Save lffg/fe5ef7ff8941de903cbebb37ae976e45 to your computer and use it in GitHub Desktop.
Save lffg/fe5ef7ff8941de903cbebb37ae976e45 to your computer and use it in GitHub Desktop.
function* enumerate(iterable) {
let i = 0;
for (const item of iterable) {
yield [i++, item];
}
}
function* withIsLast(iterable) {
const iterator = iterable[Symbol.iterator]();
let curr = iterator.next();
let next = iterator.next();
while (!curr.done) {
yield [next.done, curr.value];
[curr, next] = [next, iterator.next()];
}
}
for (const [isLast, [i, char]] of withIsLast(enumerate("hello"))) {
console.log(isLast ? 'last:' : '', i, char);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment