lua-users home
lua-l archive

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


Thanks,

It's example code to keep the email short and simple. I understand I could have done it the way you described.

I wanted to learn how to write an iterator. I am an experienced C and Lisp/Scheme programmer.

Best,

leb

At 2:38 AM -0400 10/23/09, Sean Conner wrote:
>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


-- 
leb@iridescent.org