为什么这会导致代码失败?

时间:2012-08-22 10:21:18

标签: go

我已经用FP风格写了一些代码来生成素数:

package main
import (
    "fmt"
)

func gen_number_stream() func() (int, bool) {
    i := 1
    return func() (int, bool) {
        i += 1
        return i, true
    }
}

func filter_stream(stream func() (int, bool), f func(int) bool) func() (int, bool) {
    return func() (int, bool) {
        for i, ok := stream(); ok; i, ok = stream() {
            if f(i) {
                return i, true
            }
        }
    return 0, false
    }
}

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)
            return p, true
        }
        return 0, false
    }
}


func take(stream func() (int, bool), n int) func() (int, bool) {
    return func() (int, bool) {
        if n > 0 {
            n -= 1
            return stream()
        }
        return 0, false
    }
}

func main() {

    primes := take(sieve(gen_number_stream()), 50)

    for i, ok := primes(); ok; i, ok = primes() {
        fmt.Println(i)
    }

}

当我运行此代码时,它变得越来越慢,最终得到如下运行时错误:

runtime: out of memory:  [...]

这是python代码的一个版本,它运行正常:

def gen_numbers():
    i = 2
    while True:
        yield i
        i += 1

def sieve(stream):
    p =  stream.next()
    yield p
    for i in sieve( i for i in stream if i % p != 0 ):
        yield i

def take(stream,n):
    for i,s in enumerate(stream):
        if i == 50: break
        yield s

def main():
    for i in take(sieve(gen_numbers()),50):
        print i


if __name__ == '__main__':
    main()

我想知道为什么以及如何解决它。这是我的代码或golang编译器的问题吗? 谢谢!

PS:抱歉我的英语很差。

2 个答案:

答案 0 :(得分:2)

问题是你的筛选功能是递归的。我怀疑你是通过在循环中递归地调用筛子来吹嘘你的堆栈。

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
    }
}

答案 1 :(得分:1)

您重复使用流

    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 })