gpt4 book ai didi

julia - 如何在 Julia 中实现迭代器?

转载 作者:行者123 更新时间:2023-12-01 15:44:33 25 4
gpt4 key购买 nike

我正在尝试在 Julia 中实现迭代器,但是当 for 循环尝试调用 start 时出现异常已经。

这是我得到的(我运行了 include(...),然后是 using RDF ):

julia> methods(start)
# 1 method for generic function "start":
start(graph::Graph) at /Users/jbaran/src/RDF.jl/src/RDF.jl:214

julia> for x in g
println(x)
end
ERROR: `start` has no method matching start(::Graph)
in anonymous at no file
RDF 中的函数定义模块现在看起来像这样:
function start(graph::Graph)
return GraphIterator(collect(keys(graph.statements)), nothing, nothing, nothing, [], [])
end

知道我做错了什么吗?

最佳答案

不要忘记指定 Base. - 您正在向现有函数添加方法。

module MyMod
type Blah
data
end
export Blah
Base.start(b::Blah) = 1
Base.done(b::Blah,state) = length(b.data) == state-1
Base.next(b::Blah,state) = b.data[state], state+1
end
using MyMod
x = Blah([1,2,3])
for i in x
println(i)
end

这适用于 Julia 0.3。

关于julia - 如何在 Julia 中实现迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com