admin管理员组文章数量:1334424
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? )
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? )
Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Jun 23, 2012 at 3:07 JackSMTVJackSMTV 992 silver badges7 bronze badges 04 Answers
Reset to default 2Not 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 plicated cases.
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();
}
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;
}
}
In JavaScript 1.7:
function rangeGen(n) {
for (let i = 0; i < n; i++)
yield i;
}
本文标签: iterationJavaScript Is there any quotpython39s Generatorquot equivalent in JavaScriptStack Overflow
版权声明:本文标题:iteration - JavaScript: Is there any "python's Generator" equivalent in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268811a2443913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论