Skip to content

Instantly share code, notes, and snippets.

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 indongyoo/8c79edd48d1c2e2814336242c45d35b1 to your computer and use it in GitHub Desktop.
Save indongyoo/8c79edd48d1c2e2814336242c45d35b1 to your computer and use it in GitHub Desktop.
function reverseIterator(list) {
var cur = list.length;
return {
[Symbol.iterator]: function() { return this; },
next: () => cur-- > 0 ?
{ value: list[cur], done: false } :
{ value: undefined, done: true }
}
}
for (const val of reverseIterator([1, 2])) log(val);
// 2
// 1
// Generator
function *reverseIterator(list) {
var cur = list.length;
while (cur--) yield list[cur];
}
for (const val of reverseIterator([1, 2])) log(val);
// 2
// 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment