Last active
May 14, 2019 10:31
-
-
Save 73nko/5e956e44de5522797729d4d6c12ba8bf to your computer and use it in GitHub Desktop.
Reverse loop array with iterables.
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
class IterableBackwards { | |
constructor(xs) { | |
this._xs = xs; | |
this._i = xs.length; | |
} | |
[Symbol.iterator]() { return this; } | |
next() { | |
return (this._i <= 0) | |
? { done: true, value: undefined } | |
: { done: false, value: this._xs[--this._i] }; | |
} | |
} | |
const xs = ['A1', 'A2', 'A3']; | |
for (const x of new IterableBackwards(xs)) { | |
console.info(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment