3 ms·
That looks good, and I think putting these in a prominent place of the Lua documentation (along with a notice that the code is public domain) would help us who
by strenholme 7y ago
That looks good, and I think putting these in a prominent place of the Lua documentation (along with a notice that the code is public domain) would help us who are used to the AWK/Perl/Python/PHP way of having “for” natively traverse a list without needing a complicated list-to-iterator function that uses function closure (i.e. the iterator function remembers the value “i” -- I’m writing this for the lurkers because code like this can be difficult to follow).
One honest question: Is there any reason why the function factory (i.e. a function which returns a function) which converts a list (Actually, table with ascending integer indexes) in to an iterator Lua can use with “for” returns both the element and the entire table here? Here is the code I am asking about:
return function (t)
i = i + 1
return t[i]
end, t
I’m curious why we’re returning both the table element for the iterator and the entire table.