Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cycle {
- constructor(iterable) {
- const arr = [...iterable]
- this.size = arr.length
- this.cycle = function* () {
- let i = 0
- while (true) {
- i = (i + (yield arr[i])) % arr.length
- }
- }()
- }
- *[Symbol.iterator]() {
- yield* Array.from(Array(this.size), (_, i) => this.cycle.next(+!!i).value)
- }
- prev() {
- return this.cycle.next(-1).value
- }
- next() {
- return this.cycle.next(1).value
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement