@JeffreySarnoff (Jeffrey Sarnoff) posted a new topic, subject "`iterate` returning iter, next-state and iter's original length". It read:

   I am using a custom iterator that delegates into a struct to obtain the iterative grist, say a vector. I think it is appropriate to check if the current state (like an index) exceeds the length of the delegatee.
    
    I do not want to recalculate the vector's length at each iteration, and keeping the length in the struct does not work for external reasons.
    
    What is an appropriate way to define `iterate` so that the next state returned is a tuple `(total_length, next_index)` and have it all work? I would prefer that the state stay simple (a two tuple) rather than embed both in a struct.

@Tamas_Papp (Tamas Papp) replied

    [code]
        struct Foo{T <: AbstractVector} # type for MWE
            v8b7c:f320:99b9:690f:4595:cd17:293a:c069T
        end
        
        function Base.length(8b7c:f320:99b9:690f:4595:cd17:293a:c069Foo)
            @info "length was called, and it was darn expensive"
            length(c.v)
        end
        
        function Base.iterate(8b7c:f320:99b9:690f:4595:cd17:293a:c069Foo)
            len = length(c)
            len > 0 || return nothing
            (c.v[1], (2, len))
        end
        
        Base.iterate(8b7c:f320:99b9:690f:4595:cd17:293a:c069Foo, (i, len)) = i > len ? nothing : (c.v[i], (i+1, len))
        
    [/code]
    
    then
    [code] 
        julia> foreach(println, Foo(1:10))
        [ Info: length was called, and it was darn expensive
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        
    [/code]
    
    But that said, I would just define a wrapper type that caches the length.

To which @dpsanders (David P. Sanders) replied

    Doesn't a vector know its length?

Then @JeffreySarnoff (Jeffrey Sarnoff) said

    strings do not know their length  
    although I am not using strings here
    
    You may have resolved this as Julia does, often – problem? no, its no problem.
    
    The length I need is several layers deep into substructure while iterating on `x` length(`x.partsofx.vecparts.bluevec`). I don't believe the levels of dot indirection are very costly – I do believe they are not ameanable to compile time prelocation.

After which, @JeffreySarnoff (Jeffrey Sarnoff) added

    thanks for that … I was looking for `(c, (i, len))`
    
    I'll benchmark both approaches.


{'source': 'discourse', 'url': 'https://discourse.julialang.org/t/iterate-returning-iter-next-state-and-iters-original-length/14656', 'title': "`iterate` returning iter, next-state and iter's original length", 'views': 353, 'has_accepted_answer': False, 'word_count': 301, 'like_count': 0, 'reply_count': 2, 'participant_count': 3, 'posts': 5, 'tags': '', 'author': '@JeffreySarnoff (Jeffrey Sarnoff)', 'participants': '@JeffreySarnoff (Jeffrey Sarnoff), @Tamas_Papp (Tamas Papp), @dpsanders (David P. Sanders)', 'image_url': [None], 'created_at': '2018-09-07T08:49:07.136Z', 'last_posted_at': '2018-09-07T09:37:47.769Z'}