• Subject: RE: Syntactic sugar cravings
• From: Philippe Lhoste <PhiLho@...>
• Date: Tue, 24 Jul 2001 16:56:32 +0200 (MEST)

```Jay Carlson wrote:
> I'm getting tired of typing
>
> for i=1,getn(l) do
>   local v=l[i]
>   [...]
> end

First, a reaction to such code (that I see often, even by Lua creators):
from my old 8-bit background, I tend to over-optimize, and I see this as clumsy
as the C loop:
for (i = 0; i < strlen(s); i++)
where s isn't changed.

You can write:
ln = getn(list)
for i = 1, ln do
local v = list[i]
end

Of course, you only gain milliseconds on an array of 100000 elements, but it
is more satisfying (at least for me ;-)

> It makes the loop look more complicated than it is, harder to
> understand at
> first glance etc.  What I'd like is something like
>
> for i,v in list l do
>   [...]
> end
>
> with whatever surface syntax makes sense.  "for i,v in l[] do" or "forlist
> i,v in l do" or whatever.  I guess "for i,v inlist l do" would have the
> fewest grammar implications.

I believe, and I agree with them, that Lua creators avoid whenever possible
to create new keywords, because they can already used in some program.

I may be off target, but it seems to me that it looks like the foreachi
function:

>>>
foreachi (table, func)
Executes the given func over the numerical indices of table. For each index,
the function is called with the index and respective value as arguments.
Indices are visited in sequential order, from 1 to n, where n is the result of
getn(table) (see Section 6.1). If the function returns any non-nil value, then
the loop is broken, and this value is returned as the final value of
foreachi. This function could be defined in Lua:
function foreachi (t, f)
for i=1,getn(t) do
local res = f(i, t[i])
if res then return res end
end
end
<<<

Regards.

--
--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--

Sent through GMX FreeMail - http://www.gmx.net

```