jajaperson / iterable-utilities

A bunch of utilities for working with iterables, many inspired by the native array methods.
https://deno.land/x/iter
MIT License
8 stars 1 forks source link

Methods should not be generator functions #1

Closed jajaperson closed 3 years ago

jajaperson commented 3 years ago

Using generator functions means that an iterable can only be used once. To demonstrate

function* generator() {
  let index = 0;
  while (true) {
    yield index++;
  }
}

const g = generator()

g.next().value // 0
g.next().value // 1
g[Symbol.iterator]().next().value // 2

Instead, methods should be generator functions that return an object with the iterable protocol.