Lua 5.2 and continue

classic Classic list List threaded Threaded
66 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

Lua 5.2 and continue

Joshua Jensen
I'm assuming (from the recent message referring to 'continue') that Lua
5.2 does not have a continue keyword?  I use continue almost every time
I write a script, and it would sure be nice to have it an integral part
of the language.  The continue patch works fantastic for me.

Thanks.

Josh
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Linker
You can just put the loop into a function replacing "continue" with "return".



On Thu, Jan 14, 2010 at 10:18, Joshua Jensen <[hidden email]> wrote:
I'm assuming (from the recent message referring to 'continue') that Lua 5.2 does not have a continue keyword?  I use continue almost every time I write a script, and it would sure be nice to have it an integral part of the language.  The continue patch works fantastic for me.

Thanks.

Josh



--
Regards,
Linker Lin
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Ben Kelly
Linker wrote:
> You can just put the loop into a function replacing "continue" with
> "return".
>
And add some more code to distinguish return-as-break and
return-as-continue, and before you know it the loop is looking quite
ugly and you're eyeing Metalua speculatively.
        Ben
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Pan, Shi Zhu
>> You can just put the loop into a function replacing "continue" with
>> "return".
>>
> And add some more code to distinguish return-as-break and
> return-as-continue, and before you know it the loop is looking quite
> ugly and you're eyeing Metalua speculatively.

in a function you can use error() for break, return for continue.

function foobar_with_continue_support()
    for i = 1,9 do
        if aaa then
            break
        end
        if bbb then
            continue
        end
    end
end

function foobar_without_continue_support()
    for i = 1,9 do
        if not pcall(function
            if aaa then
                error()
            end
            if bbb then
                return
            end
            end) then
            break
        end
    end
end
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Shmuel Zeigerman
In reply to this post by Joshua Jensen
Joshua Jensen wrote:
> I'm assuming (from the recent message referring to 'continue') that Lua
> 5.2 does not have a continue keyword? I use continue almost every time I
> write a script, and it would sure be nice to have it an integral part of
> the language. The continue patch works fantastic for me.

I'm not using the continue patch but would be very happy to see
`continue' in stock Lua. (Just my +1).

--
Shmuel
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Nevin Flanagan
In reply to this post by Joshua Jensen

On Jan 13, 2010, at 9:18 PM, Joshua Jensen wrote:

> I'm assuming (from the recent message referring to 'continue') that Lua 5.2 does not have a continue keyword?  I use continue almost every time I write a script, and it would sure be nice to have it an integral part of the language.  The continue patch works fantastic for me.

While I understand that the phrasing can be noticeably prettier and easier to maintain in some circumstances, is there something "if condition then continue end" does that "if not condition then rest_of_loop end" can't?
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Shmuel Zeigerman
Nevin Flanagan wrote:
>
> While I understand that the phrasing can be noticeably prettier and easier to maintain in some circumstances, is there something "if condition then continue end" does that "if not condition then rest_of_loop end" can't?

Like `break', `continue' can reside inside (deeply) nested if-then
constructs, in multiple places. (Your question kind of assumes that
`continue' can be only called from the outermost loop level).

while loop() do
   if c1() then
     ......
     if c2() then
        ......
     else
       continue
     end
   end
end

--
Shmuel
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

GrayFace
In reply to this post by Pan, Shi Zhu
Well that's the worst thing one may think of. Unclear, unsuited and a
huge performance hit, especially when error is implemented using
exception, that is if Lua is compiled as C++.
To support 'break' you can use 'return true' in place of 'error'. The
real problem is when there's a need for 'return' too:

function foobar_with_continue_support()
     for i = 1,9 do
         if aaa then
             break
         end
         if bbb then
             continue
         end
         if ccc then
             return ccc()
         end
     end
end


On 14.01.2010 8:50, pan shizhu wrote:

>>> You can just put the loop into a function replacing "continue" with
>>> "return".
>>>
>>>        
>> And add some more code to distinguish return-as-break and
>> return-as-continue, and before you know it the loop is looking quite
>> ugly and you're eyeing Metalua speculatively.
>>      
> in a function you can use error() for break, return for continue.
>
> function foobar_with_continue_support()
>      for i = 1,9 do
>          if aaa then
>              break
>          end
>          if bbb then
>              continue
>          end
>      end
> end
>
> function foobar_without_continue_support()
>      for i = 1,9 do
>          if not pcall(function
>              if aaa then
>                  error()
>              end
>              if bbb then
>                  return
>              end
>              end) then
>              break
>          end
>      end
> end
>
>
>
>    

--

___________________________________________
Best regards,
Sergey Rozhenko                 mailto:[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Theodor-Iulian Ciobanu
In reply to this post by Joshua Jensen
On Wed, 13 Jan 2010 19:18:49 -0700
Joshua Jensen <[hidden email]> wrote:

> I'm assuming (from the recent message referring to 'continue') that
> Lua 5.2 does not have a continue keyword?  I use continue almost
> every time I write a script, and it would sure be nice to have it an
> integral part of the language.  The continue patch works fantastic
> for me.

There's a mail I somehow missed a couple of days ago when I asked about
'continue' as well:
http://lua-users.org/lists/lua-l/2006-12/msg00441.html
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

David Kastrup
In reply to this post by Ben Kelly
Ben Kelly <[hidden email]> writes:

> Linker wrote:
>> You can just put the loop into a function replacing "continue" with
>> "return".
>>
> And add some more code to distinguish return-as-break and
> return-as-continue, and before you know it the loop is looking quite
> ugly and you're eyeing Metalua speculatively.

i = 1
while i<10 and
  (function()
    if i%2 == 1 then return true end
    if i == 8 then return false end
    print(i)
    return true
  end)() do
  i = i+1
end

--
David Kastrup

Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

eugeny gladkih
In reply to this post by Nevin Flanagan
Nevin Flanagan wrote:
> On Jan 13, 2010, at 9:18 PM, Joshua Jensen wrote:
>
>> I'm assuming (from the recent message referring to 'continue') that Lua 5.2 does not have a continue keyword?  I use continue almost every time I write a script, and it would sure be nice to have it an integral part of the language.  The continue patch works fantastic for me.
>
> While I understand that the phrasing can be noticeably prettier and easier to maintain in some circumstances, is there something "if condition then continue end" does that "if not condition then rest_of_loop end" can't?

example 1:

for ... do
   if c1 then do
     ...
     if c2 then do
       ...
       if c3 then do
         ...
         if c4 then do
           ...
           if c5 then do
             ...
             if c6 then do
               ...
             end
           end
         end
       end
     end
   end
end

example 2:

for ... do
   if not c1 then do continue end
   ...
   if not c2 then do continue end
   ...
   if not c3 then do continue end
   ...
   if not c4 then do continue end
   ...
   if not c5 then do continue end
   ...
   if not c6 then do continue end
end

which example is more clean and more elegant looking? yep, the second
one is.

Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Alexander Gladysh
In reply to this post by David Kastrup
>> Linker wrote:
>>> You can just put the loop into a function replacing "continue" with
>>> "return".

>> And add some more code to distinguish return-as-break and
>> return-as-continue, and before you know it the loop is looking quite
>> ugly and you're eyeing Metalua speculatively.

> i = 1
> while i<10 and
>  (function()
>    if i%2 == 1 then return true end
>    if i == 8 then return false end
>    print(i)
>    return true
>  end)() do
>  i = i+1
> end

Ouch. Do you realize you're creating a closure on each loop iteration?

Alexander.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Alexander Gladysh
In reply to this post by eugeny gladkih
> example 1:

> for ... do
>  if c1 then do
>    ...
>    if c2 then do
>      ...
>      if c3 then do
>        ...
>        if c4 then do
>          ...
>          if c5 then do
>            ...
>            if c6 then do
>              ...
>            end
>          end
>        end
>      end
>    end
>  end
> end

> example 2:

> for ... do
>  if not c1 then do continue end
>  ...
>  if not c2 then do continue end
>  ...
>  if not c3 then do continue end
>  ...
>  if not c4 then do continue end
>  ...
>  if not c5 then do continue end
>  ...
>  if not c6 then do continue end
> end

> which example is more clean and more elegant looking? yep, the second one
> is.

I'd say neither are clean and elegant looking. Such complex loop
bodies are to be refactored. If you can't refactor it to a set of
functions due to performance considerations, then elegance should not
be issue.

Alexander.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

steve donovan
On Thu, Jan 14, 2010 at 11:02 AM, Alexander Gladysh <[hidden email]> wrote:
> bodies are to be refactored. If you can't refactor it to a set of
> functions due to performance considerations, then elegance should not
> be issue.

Sigh, that's life in the embedded world!  There's always a cost to
elegance, cf 'abstraction penalty'.

Although, I much prefer code without excessive indentation and
'arrows', especially since the logic here is essentially linear.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Cosmin Apreutesei
In reply to this post by Pan, Shi Zhu
> in a function you can use error() for break, return for continue.

Oh boy.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Romulo
In reply to this post by Pan, Shi Zhu
On Thu, Jan 14, 2010 at 12:50 AM, pan shizhu <[hidden email]> wrote:
> in a function you can use error() for break, return for continue.

Ouch.

I usually just use a nested repeat:

for i = 1, 10 do repeat
    if i % 3 == 0 then
        break -- continue
    end
    print( i )
until true end -- poetic, uh?

The only problem is when you _do_ want to break the outer loop.

--rb
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Theodor-Iulian Ciobanu
On Thu, 14 Jan 2010 11:22:17 -0200
Romulo <[hidden email]> wrote:

> The only problem is when you _do_ want to break the outer loop.

I suppose you're looking for something more 'poetic' than:

for i = 1, 10 do
    local breakfor = false
    repeat
        ...
    until true
    if (breakfor == true) then
        break
    end
end
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Romulo
On Thu, Jan 14, 2010 at 11:44 AM, Theodor-Iulian Ciobanu
<[hidden email]> wrote:
> On Thu, 14 Jan 2010 11:22:17 -0200
> Romulo <[hidden email]> wrote:
>
>> The only problem is when you _do_ want to break the outer loop.
>
> I suppose you're looking for something more 'poetic' than:

For poetic I meant "until true end". Sorry for not being clear.

Regarding the "breakfor", when I need to use continue and break on the
same loop, I end up refactoring the code.I know I should do the same
for the continue, but it is just plain convenient sometimes.

--rb
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

dcharno
In reply to this post by Alexander Gladysh
Alexander Gladysh wrote:

>> example 2:
>
>> for ... do
>>  if not c1 then do continue end
>>  ...
>>  if not c2 then do continue end
>>  ...
>>  if not c3 then do continue end
>>  ...
>>  if not c4 then do continue end
>>  ...
>>  if not c5 then do continue end
>>  ...
>>  if not c6 then do continue end
>> end
>
>> which example is more clean and more elegant looking? yep, the second one
>> is.
>
> I'd say neither are clean and elegant looking. Such complex loop
> bodies are to be refactored. If you can't refactor it to a set of
> functions due to performance considerations, then elegance should not
> be issue.

The second form is clearer especially if you are writing code with lots
of validation stages or interlocks.  "Refactoring" or hiding a bunch of
stages in a separate function just so the loop looks cute isn't always
the right solution.  continue, like goto, is often misunderstood and
under appreciated.

Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.2 and continue

Sean Conner
In reply to this post by Nevin Flanagan
It was thus said that the Great Nevin Flanagan once stated:

>
> On Jan 13, 2010, at 9:18 PM, Joshua Jensen wrote:
>
> > I'm assuming (from the recent message referring to 'continue') that Lua
> > 5.2 does not have a continue keyword?  I use continue almost every time
> > I write a script, and it would sure be nice to have it an integral part
> > of the language.  The continue patch works fantastic for me.
>
> While I understand that the phrasing can be noticeably prettier and easier
> to maintain in some circumstances, is there something "if condition then
> continue end" does that "if not condition then rest_of_loop end" can't?

  From some C code I've written:

void handle_sigchld(void)
{
  pid_t child;
  int   ret;
  int   status;

  while(1)
  {
    child = waitpid(-1,&status,WNOHANG);

    if (child == (pid_t)-1)
    {
      if (errno == ECHILD) return;
      if (errno == EINTR) continue;
      (*cv_report)(LOG_ERR,"$","waitpid() returned %a",strerror(errno));
      return;
    }

    /* rest of function snipped */
  }
}

  Due to the nature of C and the reporting of errors from system calls, I'm
not sure how I would restructure this to remove the use of "continue" (just
for the unititiated---waitpid() returns -1 on error, with the actual error
in the global variable errno [1]; when errno is EINTR that means that the
system call was interrupted and you should try your system call again).  The
intent of this code is to wait for all pending children processes to end
(there may be more than one), thus the loop.  

  Hmmm ... using Lua (and assuming the API exists)

        while true do
          child,status,error = waitpid(-1,WNOHANG)
          if error == 0 then
            -- do success case
          elseif error == ECHILD then
            return
          elseif error ~= EINTR then
            log(error)
            return
          end
        end

  I guess with multiple value return, I might not need continue (although
there have been a few times I've missed it, but I've forgotten under what
circumstances it's been).  

  I do, however, like the ability of continue to avoid an additional level
of nesting that I might otherwise need.

        while true do
          child,status,error = waitpid(-1,WNOHANG)

          if error ~= 0 then
            if error == EINTR  then continue end
            if error == ECHILD then return end
            log(error)
            return
          end

          -- do success case
        end

  To me this looks cleaner.

  -spc (I think I got the first Lua fragment correct ... )

[1] To be pedantic, errno returns a "modifiable lvalue"

1234