栀子花开是离别 发表于 2024-2-27 14:18:46

JavaScript使用高阶生成器进行过滤以生成素数

生成器大家都知道是怎么一回事,但是高阶生成器又是什么东西呢?今天小编就带着大家来一起了解一下吧……
先上代码:
function* numFrom(start: number) {
    while (true) yield start++
}

function* filter(nums: Generator<number>, prime: number) {
    for (const n of nums) if (n % prime !== 0) yield n
}

let cur = numFrom(2)

for (let i = 0; i < 10; i++) {
    const prime = cur.next()
    if (!prime.done) {
      console.log(i, ':', prime.value)
      cur = filter(cur, prime.value)
    }
}结果:
0 : 21 : 32 : 53 : 74 : 115 : 136 : 177 : 198 : 239 : 29其中函数 filter 传入一个生成器,返回一个新的生成器,我们可以把它称为高阶生成器。
function* filter(nums: Generator<number>, prime: number) {
    for (const n of nums) if (n % prime !== 0) yield n
}它的作用是过滤掉 nums: Generator<number> 生成的数中能被 prime 整除的数:
const gen = filter(numFrom(10), 3)
for (let i = 0; i < 10; i++) {
    const n = gen.next()
    if (!n.done) console.log(n.value)
}结果:
10111314161719202223可以看到,3 的倍数,12、15、18、21 已经被过滤掉了。
如果我们再把函数返回值作为参数传给 filter,就能过滤掉多个值了:
let gen = filter(numFrom(10), 3) // 从 10 开始,不能被 3 整除的数
gen = filter(gen, 5) // 从 10 开始,不能被 3 和 5 整除的数
...只要我们不断把当前的生成器和产生的素数传入,我们就能够实现过滤,相当于使用筛法生成素数。
let cur = numFrom(2)

for (let i = 0; i < 10; i++) {
    const prime = cur.next()
    if (!prime.done) {
      console.log(i, ':', prime.value)
      cur = filter(cur, prime.value) // 产生新的生成器
    }
}numFrom(2) -> filter(2) -> filter(3) -> filter(5) -> filter(7) ...如果还不太清除,可以在控制台打印详细过程:
function* numFrom(start: number) {    while (true) {      console.log('\nnum yield', start)      yield start++    }}let filterDeep = 0function* filter(nums: Generator<number>, prime: number) {    const deep = filterDeep++    for (const n of nums)      if (n % prime !== 0) {            console.log(`filter yield[${deep}] ${n}`)            yield n      } else console.log(`filtered[${deep}] ${n} by ${prime}`)}let cur = numFrom(2)for (let i = 0; i < 10; i++) {    const prime = cur.next()    if (!prime.done) {      console.log(i, ':', prime.value)      cur = filter(cur, prime.value)    }}结果:
num yield 20 : 2num yield 3filter yield 31 : 3num yield 4filtered 4 by 2num yield 5filter yield 5filter yield 52 : 5num yield 6filtered 6 by 2num yield 7filter yield 7filter yield 7filter yield 73 : 7num yield 8filtered 8 by 2num yield 9filter yield 9filtered 9 by 3num yield 10filtered 10 by 2num yield 11filter yield 11filter yield 11filter yield 11filter yield 114 : 11num yield 12filtered 12 by 2num yield 13filter yield 13filter yield 13filter yield 13filter yield 13filter yield 135 : 13num yield 14filtered 14 by 2num yield 15filter yield 15filtered 15 by 3num yield 16filtered 16 by 2num yield 17filter yield 17filter yield 17filter yield 17filter yield 17filter yield 17filter yield 176 : 17num yield 18filtered 18 by 2num yield 19filter yield 19filter yield 19filter yield 19filter yield 19filter yield 19filter yield 19filter yield 197 : 19num yield 20filtered 20 by 2num yield 21filter yield 21filtered 21 by 3num yield 22filtered 22 by 2num yield 23filter yield 23filter yield 23filter yield 23filter yield 23filter yield 23filter yield 23filter yield 23filter yield 238 : 23num yield 24filtered 24 by 2num yield 25filter yield 25filter yield 25filtered 25 by 5num yield 26filtered 26 by 2num yield 27filter yield 27filtered 27 by 3num yield 28filtered 28 by 2num yield 29filter yield 29filter yield 29filter yield 29filter yield 29filter yield 29filter yield 29filter yield 29filter yield 29filter yield 299 : 29参考:
通道 - Kotlin 官方文档 https://book.kotlincn.net/text/channels.html
以上就是JavaScript使用高阶生成器进行过滤以生成素数的详细内容,更多关于JavaScript高阶生成器的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/javascript/316242r03.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: JavaScript使用高阶生成器进行过滤以生成素数