tidy way to write a repeat-until in golang

1,987 views
Skip to first unread message

xiio...@gmail.com

unread,
Oct 7, 2016, 7:25:02 PM10/7/16
to golang-nuts
Any suggestions on a way to write a repeat until loop equivalent in golang that looks tidy

currently I do :
ib := true
for i := a.X; i != b.X || ib; i = i + b.X - a.X {
ib = false
//stuff
}
 
for loops that need to be executed once under all conditions..

maybe there's neater or better way ?
 


Jonathan

unread,
Oct 7, 2016, 7:44:59 PM10/7/16
to golang-nuts, xiio...@gmail.com

for i := a.X;; i = i + b.X - a.X {
//stuff
                                             if i != b.x {
                              break

Michael Jones

unread,
Oct 7, 2016, 7:56:05 PM10/7/16
to Jonathan, golang-nuts, xiio...@gmail.com

It is a significant frustration for me, actually.

 

What I do is to leave out the test completely at the top and put it at the bottom , Just as you show. Comes up often in linked-list traversal and math code.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan Cronin

unread,
Oct 7, 2016, 8:19:52 PM10/7/16
to Michael Jones, golang-nuts, xiio...@gmail.com
On Oct 7, 2016, at 7:55 PM, Michael Jones <michae...@gmail.com> wrote:

It is a significant frustration for me, actually.

Yup.  I feel like I waste time writing clear code for this case, because I spend time thinking how to avoid the bottom test.
(And I forgot to invert the test for the if clause.)
 
What I do is to leave out the test completely at the top and put it at the bottom , Just as you show. Comes up often in linked-list traversal and math code.

How about an optional fourth clause for do-until? :)

for i := a.X;; i = i + b.X - a.X; i != b.x {
    //stuff
}




xiio...@gmail.com

unread,
Oct 8, 2016, 9:34:33 AM10/8/16
to golang-nuts, xiio...@gmail.com
If you can stomach GOTO the code :

i := a
loop1: //stuff
    //..fmt.Println(i, a, b)
    //stuff
    if i != b {
        i = i + b - a
        goto loop1
    }
 
should compile to something fairly sane without the extra variables and 'ifs'

..but it lacks local scope, and the indentation isn't helpful. It's not good to read when there are multiple loops like a 3d array case.

Egon

unread,
Oct 8, 2016, 3:19:37 PM10/8/16
to golang-nuts, xiio...@gmail.com
In this code you could use empty blocks, e.g.:

{
i := 0
loop:
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

or:

  i := 0
{
loop:
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

or:

i := 0
loop:
{
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

xiio...@gmail.com

unread,
Oct 8, 2016, 5:22:23 PM10/8/16
to golang-nuts, xiio...@gmail.com


On Saturday, 8 October 2016 20:19:37 UTC+1, Egon wrote:


In this code you could use empty blocks, e.g.:

{
i := 0
loop:
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

or:

  i := 0
{
loop:
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

or:

i := 0
loop:
{
fmt.Println(i)
if i < 10 {
i++
goto loop
}
}

Yeah I somehow forgot I could arbitrarily use brackets for scope (and fmt indentation) - I guess the "i" should be local to the loop so the first way seems to make the most sense.

In the short loop case the 'goto' form is much faster (2x) than the one with the boolean, and about the same as the example with the break conditions in the second if - which reads ok.

I suppose a go-ish form of repeat-until or do-while would look something like this

repeat i := a.X {
repeat j := a.Y {
repeat k := a.Z {

//fmt.Println(" xyz ", i, j, k)

} until k == b.Z; k = k + b.Z - a.Z
} until j == b.Y; j = j + b.Y - a.Y
} until i == b.X; i = i + b.X - a.X

 

Gary Scarr

unread,
Oct 8, 2016, 11:15:51 PM10/8/16
to golang-nuts

I prefer to show the intent of the bool with something like
   
for done :=false;!done;{

        //stuff

    done = xxx // done = expr in until(expr)

    }





On Friday, October 7, 2016 at 7:25:02 PM UTC-4, xiio...@gmail.com wrote:

Val

unread,
Oct 9, 2016, 6:30:48 AM10/9/16
to golang-nuts
Hi Garry, I like the use of a bool loop variable to show intent.  So I added an entry in page Do-while-loop idiom in Go.
Cheers
Val
Reply all
Reply to author
Forward
0 new messages