cycle method

Iterable<E> cycle([
  1. int? n
])

Returns a new lazy Iterable which iterates over this collection n times.

When it reaches the end, it jumps back to the beginning. Returns null n times if the collection is empty.

If n is omitted, the Iterable cycles forever.

Implementation

Iterable<E> cycle([int? n]) sync* {
  var it = iterator;
  if (!it.moveNext()) {
    return;
  }
  if (n == null) {
    yield it.current;
    while (true) {
      while (it.moveNext()) {
        yield it.current;
      }
      it = iterator;
    }
  } else {
    var count = 0;
    yield it.current;
    while (count++ < n) {
      while (it.moveNext()) {
        yield it.current;
      }
      it = iterator;
    }
  }
}