2

Is there any "python's Generator" equivalent in JavaScript?

PS: Python's Generator is very memory efficient when we need to do one time iterate through a big array, hash...
"Generators are iterables, but you can only read them once. It's because they do not store all the values in memory, they generate the values on the fly"

(Python's Generator explained in this thread: What does the "yield" keyword do in Python? )

Community
  • 1
  • 1
JackSMTV
  • 89
  • 2
  • 7
  • At the moment `yield` can be used in any browser (except for IE). So it might be useful to change the accepted answer to @matt3141's answer. (https://stackoverflow.com/a/11166522/7230293) – Kerwin Sneijders Feb 10 '22 at 19:42

4 Answers4

2

Not in a standard way. Some browsers already implement python-style generators, but they require extensions that need to be explicitly activated. There are proposals to add generators to a next version of the ECMAScript spec, but I wouldn't see that being useable very soon.

So far, the best you can do is to use the old school external iterator pattern. It is just as powerful, although it is a pain to write in more complicated cases.

hugomg
  • 68,213
  • 24
  • 160
  • 246
  • Thanks Missingno! The external iterator pattern is nice and new to me.. I'm trying to learn more about that.. – JackSMTV Jun 23 '12 at 03:57
  • Yield is comming! Now already in FireFox - soon to be in Chrome. http://wiki.ecmascript.org/doku.php?id=harmony:generators http://code.google.com/p/v8/issues/detail?id=2355 – mckoss Mar 28 '13 at 17:23
2

It's not super practical, but you can achieve the same basic effect like this:

function make_generator(start, end) {
  var i=start;
  return function() {
    if (i<end) {
      output = i;
      i += 1;
      return output;
    }
    else {
      return null;
    }
  }
}
var out = document.getElementById('out');
var generator = make_generator(1,10);
var g = generator();
while (g) {
  if (out.innerHTML)
    out.innerHTML = out.innerHTML + '<br>' + g;
  else
    out.innerHTML = g;
  g = generator();
}
cbare
  • 12,060
  • 8
  • 56
  • 63
  • 1
    I prefer the approaches of returning a custom object or throwing an exception to signal the end of iterations. Those allow you to iterate over things that contain null or undefined members as well. – hugomg Jun 23 '12 at 05:12
1

In JavaScript 1.7:

function rangeGen(n) {
    for (let i = 0; i < n; i++)
        yield i;
}
matt3141
  • 4,303
  • 1
  • 19
  • 24
  • Thanks Matt, but Yield only supported in Firefox with a tag – JackSMTV Jun 23 '12 at 04:04
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield Shows `yield` is widely supported now. But not by IE. – pds Mar 23 '18 at 04:41
1

You can use the yield keyword. You can read more about it here (MDN).

The yield keyword is used to pause and resume a generator function

function* range(n) {
    for (let i = 0; i < n; i++) {
        yield i;
    }
}

const generator = range(10);

console.log(generator.next());  // { value: 0, done: false }
console.log(generator.next());  // { value: 1, done: false }
...
console.log(generator.next());  // { value: 9, done: false }
console.log(generator.next());  // { value: undefined, done: true }

Notice the * after the function keyword.

It's widely supported now: caniuse, MDN


TypeScript notation:

function* range(n: number): IterableIterator<number> {
    for (let i = 0; i < n; i++) {
        yield i;
    }
}
Kerwin Sneijders
  • 750
  • 13
  • 33