javascript - 具有收益返回的静态生成器函数的正确打字稿语法是什么?
问题描述
我正在尝试在使用生成器/收益模式的类上编写静态函数。有人可以给我一个方法签名语法的代码示例吗?具体来说,我正在尝试编写一个斐波那契生成器,但任何数字生成器都可以。
class Mathy {
static yieldFibonacci(maxValue: number = Number.POSITIVE_INFINITY): IterableIterator<number> {
let current = 0
let next = 1
while (next < maxValue) {
yield next
[(current, next)] = [next, current + next]
}
}
}
解决方案
class Mathy {
static *yieldFibonacci(maxValue: number = Number.POSITIVE_INFINITY): IterableIterator<number> {
let current = 0
let next = 1
while (next < maxValue) {
yield next
;[current, next] = [next, current + next]
}
}
}
推荐阅读
- r - 使用具有浮动顶部和底部值的剪切序列进行分组
- python - 如何从 Pandas 的数据框中选择匹配结果?
- java - 如何使用弹簧注释重新编号表的主索引?
- html - 为 HTML div 应用引导网格样式
- c# - 在 Newtonsoft 中使用 JsonProperty 自动修复命名规则违规
- powerbi - 计算同一行中的数字之间的差异 - 不同的列
- c# - 如果行/行不是以 c# 中已建立的字符开头,则跳过或删除 CSV 文件中的行/行
- devstack - 使用终端创建图像时,Devstack 中的 auth 插件密码缺少值 auth-url
- html - 为查看、编辑和删除按钮创建关系,并在 laravel 应用程序中显示相应货件的特定视图/页面
- python - 张量流 2+。使用替代激活函数和 custom_gradient