rubico icon indicating copy to clipboard operation
rubico copied to clipboard

generators and asyncGenerators should be immutable

Open richytong opened this issue 4 years ago • 1 comments

generators and generated iterators should be immutable. right now they are not; methods like fork deplete the same shared iterator.

const generateIncrements = n => function*(s) {
  for (let i = 0; i < s.length; i += n) {
    yield s.slice(i, i + n)
  }
}

fork([
  reduce(add),
])(generateIncrements(2)('aaaaaaaaaa')) // => ['aaaaaaaaaa']

fork([
  reduce(add),
  reduce(add),
])(generateIncrements(2)('aaaaaaaaaa')) // TypeError: reduce(...)(x); x cannot be empty

We should have seen ['aaaaaaaaaa', 'aaaaaaaaaa']

I believe we can do this by accessing a generator's Symbol.iterator or Symbol.asyncIterator

richytong avatar Jun 08 '20 03:06 richytong

At the moment I'm against enforcing immutability in this library

richytong avatar Nov 04 '20 03:11 richytong