我正在尝试在 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
知道我做错了什么吗?
在 Julia 1.+ 中,您应该实现:
-
Base.iterate(::YourType)
对于起始迭代,
-
Base.iterate(::YourType, state)
进行后续迭代,同时获得state
从前面的步骤。
两种方法都应该返回(result, state)
元组,除了应该返回的最后一次迭代nothing
.
在实践中,这意味着迭代x::YourType
with
for i in x
# some code
end
是写作的简写
it = iterate(x)
while it !== nothing
i, state = it
# some code
it = iterate(x, state)
end
See the manual https://docs.julialang.org/en/v1/manual/interfaces/了解详情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)