Fwiw, I found an issue with the parallelism in my program where 95% of work ended up on a single worker. After fixing it I seem to be able to scale things up as much as I want to. Perhaps the worker was so overworked it could not respond?
DrChainsaw has marked this topic as resolved.
I was reading this blog trying to understand how iterators work
I tried to adapt the following script using the iterate function in place of start (), next () and done () . With some difficulty, but in the end I succeeded.
#######
Jeff Bezanson • 8 years ago
Very nice and thorough discussion of julia iterators!
I'm surprised the fibonacci example uses mutation, since it is very naturally functional:
immutable Fibs
end
start(f::Fibs) = (0, 1)
next(f::Fibs, st) = (st[2], (st[2], st[1]+st[2]))
done(f::Fibs, st) = false
Then you can do e.g. take(Fibs(), 10)
(using the Iterators package).
#################
struct Fibs
end
Base.iterate(f::Fibs) = (0,(0, 1))
Base.iterate(f::Fibs, st) = (st[2], (st[2], st[1]+st[2]))
using IterTools
collect(takestrict(Fibs(),10))
I tried later to define a non-infinite iterator, as follows:
struct Fibn
n::Int
end
ϕ=2/(1-sqrt(5))
fb(n)=Int(trunc((ϕ^n -(-ϕ)^(-n))/sqrt(5)*(-1)^n))
f(n)=Fibn(n)
length(f::Fibn)=f.n
Base.iterate(f::Fibn) = (0,(0, 1))
Base.iterate(f::Fibn, st) = st[1] <= fb(f.n-1) ? (st[2], (st[2], st[1]+st[2])) : nothing
using IterTools
for i=f(10)
println(i)
end
I just wanted to do an exercise on iterators, it's not meant to be a solution to some real problem.
I was unable to fix things to get the collect () function to work though.
julia> collect(f(10))
ERROR: MethodError: no method matching length(::Fibn)
How should I do instead of ...?
length(f::Fibn)=f.n
Last updated: Oct 02 2023 at 04:34 UTC