lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Every now and then I reread Chapter 7 of PIL.  I tell myself: once
I fully understand that, I'll consider myself a non-newbie.  I have
not yet passed the test ...

I have a question for non-newbies, at the end of this post.  It deals
with the problem:
    Write an iterator over the values of a list.  

Yes, I know I can do it with
    for _,v in ipairs(a) do -- use v in here
        end
but I find myself forgetting that dummy variable too often.  

Yes, I know I can do it with
    for i=1,#a do -- use v=a[i] in here
but that's inconvenient when a is an expression.  

I can understand the logic of this Section 7.1 type iterator:
    values = function(a) -- iterator over values of list 'a'
        local i=0
        return function() i=i+1; return a[i] end
        end

I find the rest of Chapter 7 hard going.  So here's the question to
all you experts out there (it's the same question asked three times):
    Is this simplistic but readable iterator so very inefficient?  
    Are situations in which the difference between this and one of the 
        more sophisticated iterators of Section 7.2 etc is crucial to 
        performance, so very common?  
    Am I wrong in thinking that if what I want to use the value in 
    a nontrivial way, it matters not one whit how I iterate for it?

Dirk