Maybe look at dict.jl?

https://github.com/JuliaLang/julia/blob/master/base/dict.jl

There is a definition like:
next(t::Dict, i) = ((t.keys[i],t.vals[i]), skip_deleted(t,i+1))

function skip_deleted(h::Dict, i) L = length(h.slots) while i<=L && 
!isslotfilled(h,i) i += 1 end return i end

On Friday, May 1, 2015 at 1:07:03 PM UTC-6, Kristoffer Carlsson wrote:
>
> Hello everyone,
>
> I have what is likely a basic question about iterators in Julia.
>
> Let's say I want to implement an iterator over a vector of objects and 
> only return (or yield) the objects that have some sort property.
>
> Initially I tried something like this:
>
>
> immutable MyIter{T}
>     v::Vector{T}
> end
>
> Base.start() = 1
> Base.done(::MyIter, state) = state > length(v)
>
> function Base.next(myiter::MyIter, state)
>     if has_property(v[state])
>         return(v[state], state+1)
>     else
>         return(next(myiter, state+1))
> end
>
>
> This does not work because when I hit the last element with the property 
> the iterator will go into an infinite loop. It feels like I have to check 
> if there are any more elements
> left with the property in the done function but then I have to iterate 
> over the rest of the vector...
>
> What I basically want is in my next function to be able to say "No more 
> items to return, stop iterating". In Python I would just yield the value 
> when the property is true and raise StopIterating when I reach the end of 
> the vector. Can I do something similar in Julia?
>
> Best regards,
> Kristoffer Carlsson
>

Reply via email to