rubico
rubico copied to clipboard
generators and asyncGenerators should be immutable
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
At the moment I'm against enforcing immutability in this library