Sawtaytoes
3/9/2019 - 9:58 AM

Iterator Cache

someIterator = function() {
  const someIterator = function() {
    this.count = 0 // Internal state
    this.done = false
  }
  
  someIterator
  .prototype[Symbol.iterator] = {
    next: (
      additive,
    ) => {
      this.count = this.count + additive
      
      if (this.count > 10) {
        this.done = true
      }
      
      return this.count
    },
  }
  
  return someIterator
}
createIterator = function* () {
  let additive = 0
  let count = 0
  
  do {
    count = count + (additive || 0)
    additive = 0
    
    additive = yield count
  }
  while (count < 10)
}
const createIterator = function() {
  let count = 0 // Internal state
  
  const generator = function* () {
    let additive
    
    do {
      count = count + (additive || 0)
      additive = 0
      
      additive = yield count
    }
    while (count < 10)
  }
  
  const iterator = {
    [Symbol.iterator]: generator,
  }

  return iterator
}