lua-users home
lua-l archive

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


On Mon, 02 Nov 2009 05:25:33 +0200, Jim Pryor <lists+lua@jimpryor.net> wrote:

But if we were designing iterators from scratch,
wouldn't it be nice to give them this signature instead:

    generator(control_variable, state_var1, ...)


If <dreaming> about iterators that could have been...

for var_1, ···, var_n in explist do block end

-- could be equivalent to the code:
do
  local f, s, ... = explist  -- all values returned by explist
  while true do
local s, ..., var_1, ···, var_n = f(s, ...) -- call f with all values returned by previous iteration, assign leftmost n to control variables
    if s == nil then break end
    block
  end
end

I.e. for would not throw away extra values returned by iterator. When it returns nothing, loop would end. This would have made writing iterators for nested data structures (like filesystem) so easy...
</dreaming>