lua-users home
lua-l archive

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


Are you trying to avoid backtracking the index or keeping its previous value? How about this:

function it_paths(paths)
   local function next(state, var)
       local result
       result, state.idx = paths[state.idx], state.idx+1
       return result
   end
   local state = {idx = 1 };
   return next, state, "";
end;

Mark Feldman

Lawrence E. Bakst wrote:
1. Is there a better, shorter, cuter, or cooler way to code the example below?

2. I guess something I didn't try was to "deep copy" paths, use that as the state, and then remove one element at a time from the state.

3. This is probably FAQ, but I couldn't find much with a quick search on Google.

I miss C's pre/post inc/dec operators and I can't be the only one. Is there any way to make inc and dec functions that increment or decrement the number passed in and return the previous value? Seems difficult in a language that only has pass by value for numbers and strings.

Thanks in advance for all help and suggestions.

function it_paths(paths)
    local function next(state, var)
        if (state.idx > #paths) then return nil end;
        state.idx = state.idx + 1;
        return paths[state.idx - 1];
    end
    local state = {idx = 1};
    return next, state, "";
end;

function process_random_files(paths)
    for path in it_paths(paths) do
        print(path);
    end
end

process_random_files{"foo", "bar", "baz", "quuz"};



This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are free from computer virus or other defect.