Advertisement
Guest User

Untitled

a guest
May 30th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. class Cycle {
  2. constructor(iterable) {
  3. const arr = [...iterable]
  4. this.size = arr.length
  5. this.cycle = function* () {
  6. let i = 0
  7. while (true) {
  8. i = (i + (yield arr[i])) % arr.length
  9. }
  10. }()
  11. }
  12. *[Symbol.iterator]() {
  13. yield* Array.from(Array(this.size), (_, i) => this.cycle.next(+!!i).value)
  14. }
  15. prev() {
  16. return this.cycle.next(-1).value
  17. }
  18. next() {
  19. return this.cycle.next(1).value
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement