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

2024-03-01 0 174

生成器大家都知道是怎么一回事,但是高阶生成器又是什么东西呢?今天小编就带着大家来一起了解一下吧……

先上代码:

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 = 0
function* 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[0] 31 : 3num yield 4filtered[0] 4 by 2num yield 5filter yield[0] 5filter yield[1] 52 : 5num yield 6filtered[0] 6 by 2num yield 7filter yield[0] 7filter yield[1] 7filter yield[2] 73 : 7num yield 8filtered[0] 8 by 2num yield 9filter yield[0] 9filtered[1] 9 by 3num yield 10filtered[0] 10 by 2num yield 11filter yield[0] 11filter yield[1] 11filter yield[2] 11filter yield[3] 114 : 11num yield 12filtered[0] 12 by 2num yield 13filter yield[0] 13filter yield[1] 13filter yield[2] 13filter yield[3] 13filter yield[4] 135 : 13num yield 14filtered[0] 14 by 2num yield 15filter yield[0] 15filtered[1] 15 by 3num yield 16filtered[0] 16 by 2num yield 17filter yield[0] 17filter yield[1] 17filter yield[2] 17filter yield[3] 17filter yield[4] 17filter yield[5] 176 : 17num yield 18filtered[0] 18 by 2num yield 19filter yield[0] 19filter yield[1] 19filter yield[2] 19filter yield[3] 19filter yield[4] 19filter yield[5] 19filter yield[6] 197 : 19num yield 20filtered[0] 20 by 2num yield 21filter yield[0] 21filtered[1] 21 by 3num yield 22filtered[0] 22 by 2num yield 23filter yield[0] 23filter yield[1] 23filter yield[2] 23filter yield[3] 23filter yield[4] 23filter yield[5] 23filter yield[6] 23filter yield[7] 238 : 23num yield 24filtered[0] 24 by 2num yield 25filter yield[0] 25filter yield[1] 25filtered[2] 25 by 5num yield 26filtered[0] 26 by 2num yield 27filter yield[0] 27filtered[1] 27 by 3num yield 28filtered[0] 28 by 2num yield 29filter yield[0] 29filter yield[1] 29filter yield[2] 29filter yield[3] 29filter yield[4] 29filter yield[5] 29filter yield[6] 29filter yield[7] 29filter yield[8] 299 : 29

参考:

通道 – Kotlin 官方文档https://book.kotlincn.net/text/channels.html

以上就是JavaScript使用高阶生成器进行过滤以生成素数的详细内容,更多关于JavaScript高阶生成器的资料请关注悠久资源网其它相关文章!

您可能感兴趣的文章:

  • 一文详解JavaScript中生成器函数的妙用
  • Javascript生成器(Generator)的介绍与使用
  • js下拉菜单生成器dropMenu使用方法详解
  • js中判断一个数是不是素数的三种方法例子
  • 解析利用javascript如何判断一个数为素数

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 JavaScript JavaScript使用高阶生成器进行过滤以生成素数 https://www.u-9.cn/biancheng/javascript/181851.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务