[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Better way to code an iterator function?
- From: Sean Conner <sean@...>
- Date: Fri, 23 Oct 2009 02:38:15 -0400
It was thus said that the Great Lawrence E. Bakst once stated:
> 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"};
What's wrong with:
function process_random_files(paths)
for i=1,#paths do
print(paths[i])
end
end
Or am I missing something?
-spc