# Syntactic sugar cravings

9 messages
Open this post in threaded view
|

## Syntactic sugar cravings

 ```I'm getting tired of typing for i=1,getn(l) do local v=l[i] [...] end 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. Also, I think I'm going to implement the following in my interactive lua command loops: =2+2 is an abbreviation for _EVAL_PRINT(2+2) where _EVAL_PRINT is initially set to print. It gets a separate hook to allow for pretty-printing of tables. I considered flipping the default and using ; to prefix statements but that doesn't allow you to paste code into the loop. Jay ```
Open this post in threaded view
|

## RE: Syntactic sugar cravings

 ```>From: Philippe Lhoste [[hidden email]] >You can write: >ln = getn(list) >for i = 1, ln do > local v = list[i] >end There's no need to do this - the loop limit is evaluated only once (or so says the Reference Manual). -Virus scanned and cleared ok ```
Open this post in threaded view
|

## RE: Syntactic sugar cravings

 ```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 ```
Open this post in threaded view
|

## RE: Syntactic sugar cravings

 In reply to this post by Jay Carlson ```>> for i=1,getn(l) do > >You can write: >ln = getn(list) >for i = 1, ln do > local v = list[i] >end No need for this. In a "for" loop, the limits are computed before the loop starts. The manual says so: A for statement like for var = e1 ,e2, e3 do block end is equivalent to the code: do local var, _limit, _step = tonumber(e1), tonumber(e2), tonumber(e3) if not (var and _limit and _step) then error() end while (_step>0 and var<=_limit) or (_step<=0 and var>=_limit) do block var = var+_step end end In C it's different. :-) --lhf ```
Open this post in threaded view
|

## Re: Syntactic sugar cravings

 In reply to this post by Jay Carlson ```> Also, I think I'm going to implement the following in my interactive lua > command loops: > > =2+2 > > is an abbreviation for > > _EVAL_PRINT(2+2) This seems a nice (and simple) solution for those who use Lua as a calculator. The `=' is an easy key (no shifts involved ;-), and we do not need to change Lua (the core), only lua.c. We will consider it for 4.1 beta (alpha is almost out now). -- Roberto ```
Open this post in threaded view
|

## Re: Syntactic sugar cravings

 ```--- Roberto Ierusalimschy <[hidden email]> wrote: > > Also, I think I'm going to implement the following in my interactive lua > > command loops: > > > > =2+2 > > > > is an abbreviation for > > > > _EVAL_PRINT(2+2) > > This seems a nice (and simple) solution for those who use Lua as a calculator. > The `=' is an easy key (no shifts involved ;-), and we do not need to change > Lua (the core), only lua.c. We will consider it for 4.1 beta (alpha is almost > out now). As long as we're on the subject of syntactical sugar... I would like to see the 'then' following 'if' and 'elseif', and the 'do' following 'while' or 'for', be made optional. Those tokens are totally superfluous - making them optional would have no impact on Lua's efficiency or on existing Lua code. Besides saving keystrokes, it makes Lua code even cleaner and easier to read (IMO). Now for the ulterior motive... I used Lua to replace a scripting language in an in-house tool, which (believe it or not) had virtually identical syntax to Lua (albeit a subset of Lua). Lua requires the superfluous tokens mentioned above; the previous language did not. If those tokens can be made optional in the official source, then I can finally call our in-house variation "Lua". ;) Cheers, Eric __________________________________________________ Do You Yahoo!? Make international calls for as low as \$.04/minute with Yahoo! Messenger http://phonecard.yahoo.com/ ```
Open this post in threaded view
|

## Re: Syntactic sugar cravings

 In reply to this post by Jay Carlson ```Jay Carlson wrote: > I'm getting tired of typing > > for i=1,getn(l) do > local v=l[i] > [...] > end > > 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 A while back I made a small patch for 4.0 that takes care of this (see Power Patches page on the Lua Wiki). However my patch doesn't provide the index value. Edgar T's Sol provides exactly what you wrote but you may have a hard time picking out the necessary changes. -John ```
 ```[written in Outlook Express. :-P ] "John Belmonte" <[hidden email]> writes: > Jay Carlson wrote: > > I'm getting tired of typing > > > > for i=1,getn(l) do > > local v=l[i] > > [...] > > end > > > > 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 > > A while back I made a small patch for 4.0 that takes care of this (see Power > Patches page on the Lua Wiki). However my patch doesn't provide the index > value. Edgar T's Sol provides exactly what you wrote but you may have a > hard time picking out the necessary changes. Cool. I usually don't use the index value either. But it seemed best to keep the symmetry with the existing for-in table syntax, and use _,v to throw away when necessary. I looked at sol a little a while ago; I was impressed. I'm a little worried about how far it is away from straight lua, but that's more a political/social issue than a technical concern. Jay ```
 ```Jay Carlson wrote: > I looked at sol a little a while ago; I was impressed. I'm a > little worried about how far it is away from straight lua, but > that's more a political/social issue than a technical concern. I disagree, in most cases it is a technical concern. Lua is continually being improved. If you're using a distant fork that is not tracking those improvements, then you'll either miss out on the advances or have to apply substantial effort to track them yourself. That's why I encourage localized patches that change as little code as possible. -John ```