Last active
April 3, 2018 12:17
-
-
Save indongyoo/8c79edd48d1c2e2814336242c45d35b1 to your computer and use it in GitHub Desktop.
This file contains 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
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