peterschussheim
4/9/2017 - 3:31 PM

iterator-objects

iterator-objects

const Stack3 = () =>
  ({
    array: [],
    index: -1,
    push (value) {
      return this.array[this.index += 1] = value;
    },
    pop () {
      const value = this.array[this.index];

      this.array[this.index] = undefined;
      if (this.index >= 0) {
        this.index -= 1
      }
      return value
    },
    isEmpty () {
      return this.index < 0
    },
    [Symbol.iterator] () {
      let iterationIndex = this.index;

      return {
        next () {
          if (iterationIndex > this.index) {
            iterationIndex = this.index;
          }
          if (iterationIndex < 0) {
            return {done: true};
          }
          else {
            return {done: false, value: this.array[iterationIndex--]}
          }
        }
      }
    }
  });

const stack = Stack3();

stack.push(2000);
stack.push(10);
stack.push(5)

const collectionSum = (collection) => {
  const iterator = collection[Symbol.iterator]();

  let eachIteration,
      sum = 0;

  while ((eachIteration = iterator.next(), !eachIteration.done)) {
    sum += eachIteration.value;
  }
  return sum
}

collectionSum(stack)
const Stack2 = () =>
	({
        array: [],
        index: -1,
        push (value) {
            return this.array[this.index += 1] = value;
        },
        pop () {
            const value = this.array[this.index];
            
            this.array[this.index] = undefined;
            if (this.index >= 0) {
                this.index -= 1
            }
            return value
        },
        isEmpty () {
            return this.index < 0
        },
        [Symbol.iterator] () {
            let iterationIndex = this.index;
            
            return {
                next () {
                    if (iterationIndex >this.index) {
                        iterationIndex = this.index;
                    }
                    if (iterationIndex < 0) {
                        return { done: true };
                    } else {
                        return { done: false, value: this.array[iterationIndex--] }
                    }
                }
            }
        }
    });

const stack = Stack2();

stack.push(2000);
stack.push(10);
stack.push(5);

const collectionSum = collection => {
    const iterator = collection[Symbol.iterator]();
    
    let eachIteration,
        sum = 0;
    
    while ((eachIteration = iterator.next(), !eachIteration.done)) {
        sum += eachIteration.value;
    }
    return sum
}

let summed = collectionSum(stack);

const iterableSum = iterable => {
    let sum = 0;
    for (const num of iterable) {
        sum += num;
    }
    return sum
}

iterableSum(stack);
///A generic pattern we can use to work with ordered collections is to make the collection an iterator.  An iterator is a POJO with a next() method that is repeatedly invoked to pull out elements in proper order.

iterator-objects

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.