# What's the rationale behind "for i=a,b[,c]"?

10 messages
Open this post in threaded view
|

## What's the rationale behind "for i=a,b[,c]"?

 What's the rationale behind being able to do "for i=1,10" but not "for i=string.byte("AZ",1,2)"? (i.e. why are commas syntactically significant in this case, but not in "for x,y in next, t"?) (Ofc, if we had a range() instead of numeric for, we would be able to do "for i in range(string.byte("AZ",1,2))", and then this question would make no sense, but we don't. So, what's the rationale?) -- Disclaimer: these emails are public and can be accessed from . If you do not agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 It was thus said that the Great Soni L. once stated: > What's the rationale behind being able to do "for i=1,10" but not "for > i=string.byte("AZ",1,2)"? (i.e. why are commas syntactically significant > in this case, but not in "for x,y in next, t"?)   It's an optimization for a common case would be my guess. > (Ofc, if we had a range() instead of numeric for, we would be able to do > "for i in range(string.byte("AZ",1,2))", and then this question would > make no sense, but we don't. So, what's the rationale?) function range(a,b)   local function next(s,v)     v = v + 1     if v <= s then       return v     end   end   return next,b,a-1 end for i in range(string.byte("AZ",1,2)) do   print(i) end   -spc (So what was your question again?)
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 On 16/03/15 10:29 PM, Sean Conner wrote: > It was thus said that the Great Soni L. once stated: >> What's the rationale behind being able to do "for i=1,10" but not "for >> i=string.byte("AZ",1,2)"? (i.e. why are commas syntactically significant >> in this case, but not in "for x,y in next, t"?) >    It's an optimization for a common case would be my guess. > > Stop me from doing that just for optimization? Meh, makes no sense IMO :/ (e.g. there's no way to do t:[var]() (OP_SELF with variable) in rio lua) -- Disclaimer: these emails are public and can be accessed from . If you do not agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 In reply to this post by Soni "They/Them" L. 2015-03-17 2:14 GMT+02:00 Soni L. <[hidden email]>: > What's the rationale behind being able to do "for i=1,10" but not "for > i=string.byte("AZ",1,2)"? (i.e. why are commas syntactically significant in > this case, but not in "for x,y in next, t"?) > > (Ofc, if we had a range() instead of numeric for, we would be able to do > "for i in range(string.byte("AZ",1,2))", and then this question would make > no sense, but we don't. So, what's the rationale?) Look at the syntax definitions. Generic for has:     for namelist in explist do block end Name lists and expression lists are expected. No commas are required. If the last expression in an expression list is a function call, then all the return values enter the list. Hence the possibility of functions like pairs(). All that is prescribed is for the first expression in the list to be a function. But arithmetic for has:     for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end Twi or three comma-separated expressions are required, not an explist. It may look like an explist but it isn't. The expressions are evaluated one by one at compile time, tested for being numbers and assigned to the loop control variables. Only the first value is kept if `exp` is something with multiple values. > function demo(...) >> for i = ... , ... , ... do print(i) end >> end > demo(1,10,2) 1
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 On 17/03/15 01:24 AM, Dirk Laurie wrote: > 2015-03-17 2:14 GMT+02:00 Soni L. <[hidden email]>: > >> What's the rationale behind being able to do "for i=1,10" but not "for >> i=string.byte("AZ",1,2)"? (i.e. why are commas syntactically significant in >> this case, but not in "for x,y in next, t"?) >> >> (Ofc, if we had a range() instead of numeric for, we would be able to do >> "for i in range(string.byte("AZ",1,2))", and then this question would make >> no sense, but we don't. So, what's the rationale?) > Look at the syntax definitions. Generic for has: > >      for namelist in explist do block end > > Name lists and expression lists are expected. No commas are required. > If the last expression in an expression list is a function call, then all the > return values enter the list. Hence the possibility of functions like pairs(). > All that is prescribed is for the first expression in the list to be a function. > > But arithmetic for has: > >      for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end > > Twi or three comma-separated expressions are required, not an explist. > It may look like an explist but it isn't. The expressions are evaluated one > by one at compile time, tested for being numbers and assigned to the > loop control variables. Only the first value is kept if `exp` is something > with multiple values. > >> function demo(...) >>> for i = ... , ... , ... do print(i) end >>> end >> demo(1,10,2) > 1 > local a,b,c = 1,10,nil for i=a,b,c do print(i) end -- doesn't work because of the explicit nil :/ why's nil not a valid "no value" here? :/ So there's no easy way to do it, other than: local a,b,c = 1,10,nil if c == nil then    for i=a,b do          end else    for i=a,b,c do       -- we have to duplicate the code?!    end end Which's bad due to duplicated code -- Disclaimer: these emails are public and can be accessed from . If you do not agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 On 17 March 2015 at 21:45, Soni L. <[hidden email]> wrote: > > local a,b,c = 1,10,nil > for i=a,b,c do print(i) end -- doesn't work because of the explicit nil :/ > why's nil not a valid "no value" here? :/ > > So there's no easy way to do it, other than: > > local a,b,c = 1,10,nil > if c == nil then >   for i=a,b do >     >   end > else >   for i=a,b,c do >     -- we have to duplicate the code?! >   end > end > > Which's bad due to duplicated code > > > -- > Disclaimer: these emails are public and can be accessed from non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY. > > What about this? local a,b,c = 1,10,nil for i = a, b, c or 1 do     end
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 It was thus said that the Great Choonster TheMage once stated: > On 17 March 2015 at 21:45, Soni L. <[hidden email]> wrote: > > > > local a,b,c = 1,10,nil > > for i=a,b,c do print(i) end -- doesn't work because of the explicit nil :/ > > why's nil not a valid "no value" here? :/ > > > > So there's no easy way to do it, other than: > > > > local a,b,c = 1,10,nil > > if c == nil then > >   for i=a,b do > >     > >   end > > else > >   for i=a,b,c do > >     -- we have to duplicate the code?! > >   end > > end > > > > Which's bad due to duplicated code > > > > > > -- > > Disclaimer: these emails are public and can be accessed from > non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY. > > > > > > What about this? > > local a,b,c = 1,10,nil > for i = a, b, c or 1 do >     > end Or ... function range(low,high,delta)   local up    = function(v) return v <= high end   local down  = function(v) return v >= high end   local d   local cmp   if not delta then     if low < high then       d = 1     else       d = -1     end   else     d = delta   end   if d > 0 then     cmp = up   else     cmp = down   end   return function(s,v)     v = v + d     if cmp(v) then       return v     end   end,nil,low-d end for x in range(string.byte("AZ",1,2)) do   print(x) end for x in range(1,10,1) do print(x) end for x in range(10,1,-1) do print(x) end for x in range(string.byte("ZA",1,2),-1) do   print(x) end   -spc (And just skip the whole "for i = low,high ..." thing entirely)
Open this post in threaded view
|

## Re: What's the rationale behind "for i=a,b[,c]"?

 2015-03-17 18:57 GMT+02:00 Sean Conner <[hidden email]>: > Or ... [ some 30 lines of maypole-formatted code ] >   -spc (And just skip the whole "for i = low,high ..." thing entirely) "Was that sarcasm?" (Sheldon)