[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Better way to code an iterator function?
- From: "Lawrence E. Bakst" <ml@...>
- Date: Fri, 23 Oct 2009 01:42:26 -0400
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"};
--
leb@iridescent.org