Functional programming 使用Lazy.jl在Julia中生成惰性范围
在尝试从扩展其中一个示例时,我遇到了一个问题,即计算不是懒惰的Functional programming 使用Lazy.jl在Julia中生成惰性范围,functional-programming,julia,lazy-evaluation,Functional Programming,Julia,Lazy Evaluation,在尝试从扩展其中一个示例时,我遇到了一个问题,即计算不是懒惰的 README使用了以下示例: > esquares = @>> Lazy.range() map(x->x^2) filter(iseven); > esquares[99] 39204 我试图通过允许将过滤器指定为参数使其成为动态的,但它最终会计算一个无限列表: > squares(filt) = @lazy @>> Lazy.range() map(x->x^2) fil
README
使用了以下示例:
> esquares = @>> Lazy.range() map(x->x^2) filter(iseven);
> esquares[99]
39204
我试图通过允许将过滤器指定为参数使其成为动态的,但它最终会计算一个无限列表:
> squares(filt) = @lazy @>> Lazy.range() map(x->x^2) filter(filt);
> squares(iseven)
(4 16 36 64 100 144 196 256 324 400 484 576 676 ... # this keeps printing until interrupting...)
我还尝试:
> @lazy squares(iseven)
(4 16 36 64 100 144 196 256 324 400 484 576 676 ... # this also immediately returns the infinite list
显示惰性对象需要访问其内容(尽管当前的
show
方法是否应该更改仍有争议),这就是为什么esquares
示例中的code>非常重要
考虑到这一点,您的代码运行良好:
julia> squares(filt) = @lazy @>> Lazy.range() map(x->x^2) filter(filt) # you don't need the `@lazy` here I think
squares (generic function with 1 method)
julia> squares(iseven);
julia> squares(iseven)[99]
39204
julia> squares(isodd)[99]
38809