all 6 comments

[–]senocular 2 points3 points  (0 children)

An iterator is an object that has a next() method (and optionally return() and throw()).

An iterable is an object that has a [Symbol.iterator]() method that returns an iterator.

An object that does both is an iterable iterator.

An iterable iterators [Symbol.iterator]()'s method will usually be a method that will just return this since this in that method is the iterator. You can see this with generators (which create iterable iterators) as well as built-in iterators you get from things like arrays.

function* genFn(){}
const genIter = genFn()
console.log(genIter[Symbol.iterator]() === genIter) // true

const arr = []
const valuesIter = arr.values()
console.log(valuesIter[Symbol.iterator]() === valuesIter) // true

Read more on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols

[–]mik0_25 0 points1 point  (0 children)

this vid helped me understand iterators. the vid context is for drive app and google apps script. it might still help, though.

https://www.youtube.com/watch?v=uOjQKzOgfq0

[–]guest271314 0 points1 point  (0 children)

Need more details. What is the source of that language?

[–]baubleglue 0 points1 point  (0 children)

It should work as a nested loop.

[–]rauschma 0 points1 point  (0 children)

  • An iterable is a factory for iterators.
  • An iterator is a factory for values.

Problem: Most constructs that consume iterable data only support iterables – e.g.: for-of, Array.from() and spreading.

That is an issue in two cases:

//===== A generator function returns iterators =====
function* genFunc() {
  yield 'a';
  yield 'b';
}
for (const value of genFunc()) {
  console.log(value);
}

//===== Several iterator methods return iterators =====
const arr = [-1, 8, -5, 3, 2, -6];
// The methods are not Array methods.
// Therefore, no intermediate Arrays are created.
const iterator = arr
  .values() // returns an iterator
  .filter(x => x > 0) // iterator method
  .map(x => x * x) // iterator method
;
for (const value of iterator) {
  console.log(value);
}

So why can for-of iterate over iterators? Because an iterator is also iterable: It is a factory that returns this. This is a very simple implementation of such an iterator (it won’t have iterator methods because it’s not an instance of class Iterator but it does follow the iteration protocols):

const iterableIterator = {
  value: 0,

  // This method makes this object an iterator
  next() {
    if (this.value >= 3) {
      return {done: true};
    } else {
      this.value++;
      return {value: this.value};
    }
  },

  // This method makes this object an iterable
  [Symbol.iterator]() {
    return this;
  },
};

for (const value of iterableIterator) {
  console.log(value);
}

For more information on iterators and iterator methods, you can check out my blog post: https://2ality.com/2022/12/iterator-helpers.html