2 回答
TA贡献1815条经验 获得超13个赞
问题是您的筛选功能是递归的。我怀疑您是通过在循环中连续递归调用sieve来浪费堆栈的。
func sieve(stream func() (int, bool)) func() (int, bool) {
return func() (int, bool) {
if p, ok := stream(); ok {
remaining := filter_stream(stream, func(q int) bool { return q % p != 0 })
stream = sieve(remaining) // just keeps calling sieve recursively which eventually blows your stack.
return p, true
}
return 0, false
}
}
TA贡献1111条经验 获得超0个赞
您重用流
if p, ok := stream(); ok {
remaining := filter_stream(stream, func(q int) bool { return q % p != 0 })
但是对于每个新的“ p”您必须创建一个新的“ stream2”
if p, ok := stream(); ok {
stream2 := ....
remaining := filter_stream(stream2, func(q int) bool { return q % p != 0 })
- 2 回答
- 0 关注
- 179 浏览
添加回答
举报