functional-programming - 使用 Lazy.jl 在 Julia 中生成惰性范围
问题描述
在尝试从Lazy.jl扩展其中一个示例时,我遇到了一个评估不懒惰的问题。
使用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
的 如此重要的原因。
考虑到这一点,您的代码可以正常工作:
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
推荐阅读
- excel - 如何将excel图表中的x标签指定为“2019-01”、“2019-02”等?
- flutter - Flutter:如何处理具有空值的图像?
- c - 将“c99”循环转换为常规内容
- git - 创建合并请求后,我还可以在分支上添加提交吗?
- django - TypeError: int() 参数在迁移 django 模型期间必须是字符串
- java - 交换字符串中的字母
- html - 如何使这个 div 设备响应
- c# - 在 C# 中单击 geckoWebBrowser 中的一个链接
- python - ValueError: Error when checking input: expected conv2d_36_input to have shape (3, 32, 32) but got array with shape (1, 10, 10)
- android - 如何过滤我的 firebase 数据库并将它们添加到我的 android spinner?