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
复制代码 其中函数传入一个生成器,返回一个新的生成器,我们可以把它称为高阶生成器。- function* filter(nums: Generator<number>, prime: number) {
- for (const n of nums) if (n % prime !== 0) yield n
- }
复制代码 它的作用是过滤掉生成的数中能被整除的数:- const gen = filter(numFrom(10), 3)
- for (let i = 0; i < 10; i++) {
- const n = gen.next()
- if (!n.done) console.log(n.value)
- }
复制代码 结果:可以看到,的倍数,、、、已经被过滤掉了。
如果我们再把函数返回值作为参数传给,就能过滤掉多个值了:- 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[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高阶生成器的资料请关注脚本之家其它相关文章!
来源:https://www.jb51.net/javascript/316242r03.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|
|
|
发表于 2024-2-27 14:17:29
举报
回复
分享
|
|
|
|