lua-users home
lua-l archive

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


Philippe Lhoste wrote:
> Sorry if I don't answer correctly to your request, but I believe Lua
already
> have a mechanism for this.
>
> foreach (table, func)
> foreachi (table, func)

Sorry, I should have addressed these library functions in my post.  Well, if
foreach() is so great, why do we have a special for construct to do the same
thing?  Remember that the for command was created after these functions
already existed.  I'll bet that since for loops were introduced as a way to
iterate tables, most of us don't use foreach() anymore.  Having to create
function objects can sometimes make code hard to follow.   So since there is
a form of for matching foreach(), why not foreachi() too?

For comparison, here is my example again using foreachi.  It's not bad in
the case of a one-statement function, but for larger functions it becomes
less readable that using a for construct.

    foreachi(list, function(i, val) print(val) end)


-John