巧用es6生成器产生质数 Posted on 2017-10-19 | In 算法 巧用es6生成器产生质数 廖雪峰的python教程”函数式编程->高阶函数->filter”中利用生成器写了一个质数生成的例子,我用es6的生成器也模仿了一个: 123456789101112131415161718192021222324252627282930313233function *oddIter() { var n = 1 while (true) { n = n + 2 yield n }}function *filter (it, n) { for (var i of it) { if (i % n > 0) { yield i } }}function *primes () { yield 2 var it = oddIter() while (true) { var n = it.next() yield n.value it = filter(it, n.value) }}for (var i of primes()) { if (i < 20) { console.log(i) } else { break }}