Repeat n times when iteration number not needed

8,764 views
Skip to first unread message

Mark Reed

unread,
Apr 2, 2015, 9:29:15 PM4/2/15
to golan...@googlegroups.com
I have a loop in my code that just needs to execute a certain number of times.  

I don't make any reference to the loop control variable anywhere in the body of the loop, but since the for clauses have to test it against the limit and increment it, I can't use _.  So instead, I just used _i, as a signal that the variable's value was unused.  Does that seem reasonable, or would it be better Go  to just leave it as bog-standard i and not worry about signalling the don't-care condition?

Caleb Spare

unread,
Apr 2, 2015, 9:35:29 PM4/2/15
to Mark Reed, golang-nuts
Just use i anyway in those cases. That's what the code in e.g. the standard library does. If I saw _i, the nonstandard naming would probably trip me up more than a loop variable that's not used within the body of the loop.

-Caleb

On Thu, Apr 2, 2015 at 6:29 PM, Mark Reed <mark...@gmail.com> wrote:
I have a loop in my code that just needs to execute a certain number of times.  

I don't make any reference to the loop control variable anywhere in the body of the loop, but since the for clauses have to test it against the limit and increment it, I can't use _.  So instead, I just used _i, as a signal that the variable's value was unused.  Does that seem reasonable, or would it be better Go  to just leave it as bog-standard i and not worry about signalling the don't-care condition?

--
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.

Caleb Spare

unread,
Apr 2, 2015, 9:36:55 PM4/2/15
to Mark Reed, golang-nuts
Or you can do

for range [10]struct{}{} {
  // blah
}

(Joke; please don't do this.)

Mark J. Reed

unread,
Apr 2, 2015, 9:48:11 PM4/2/15
to Caleb Spare, golang-nuts
Well, obviously if I were going to do that I would use the iter package.  Much more legible. ;)
--
Mark J. Reed <mark...@gmail.com>

Jason Gade

unread,
Apr 3, 2015, 1:37:02 AM4/3/15
to golan...@googlegroups.com, mark...@gmail.com
Holy cow, that works. http://play.golang.org/p/wInRfj9jk_

Mind. Blown.

anl...@gmail.com

unread,
Apr 5, 2015, 1:38:12 PM4/5/15
to golan...@googlegroups.com

Rob Pike

unread,
Apr 5, 2015, 5:02:22 PM4/5/15
to anl...@gmail.com, golan...@googlegroups.com
for i := 0; i < 10; i++

Short, simple, straightforward, clear, obvious, understandable, not a puzzle, not cute, not confusing, just right, just do it.

-rob


--

Piers

unread,
Apr 6, 2015, 3:32:15 AM4/6/15
to golan...@googlegroups.com, anl...@gmail.com

On Sunday, 5 April 2015 18:38:12 UTC+1, anl...@gmail.com wrote:
http://play.golang.org/p/X6mCEsIFzI
 
There's a 'subtle' bug..

Out of interest I ran it through 'go vet' and golint and they don't report any problem. 

Is there a tool or invocation of the existing ones which does report unused function parameters?

I suppose a tool could also report 'suspicious' repeats of the same constant, e.g. 10 here, but that might get annoying :)

Dan Kortschak

unread,
Apr 6, 2015, 5:06:36 AM4/6/15
to Piers, golan...@googlegroups.com, anl...@gmail.com
It's a feature. "Any caller can have a loop counting any number of iterations that he wants so long as it is 10." – Henry Ford

atd...@gmail.com

unread,
Apr 6, 2015, 10:28:47 AM4/6/15
to golan...@googlegroups.com, goo...@o172.net, anl...@gmail.com

On Monday, April 6, 2015 at 11:06:36 AM UTC+2, kortschak wrote:
It's a feature. "Any caller can have a loop counting any number of iterations that he wants so long as it is 10." – Henry Ford


I could have sworn that quote was from Mahatma Gandhi.

Russel Winder

unread,
Apr 6, 2015, 10:44:41 AM4/6/15
to golan...@googlegroups.com
On Sun, 2015-04-05 at 14:01 -0700, Rob Pike wrote:
> for i := 0; i < 10; i++
>
> Short, simple, straightforward, clear, obvious, understandable, not a
> puzzle, not cute, not confusing, just right, just do it.
>

Whilst this is the way it has to be done in Go, I think you are wrong
on all counts. This is why things like:

10.times

for _ in 1..10

and other variants have been chosen in other languages.

Without experiments from the psychology of programming folk, making
statements about "understandable" and "not confusing" is simply
opinion not data.

--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel...@ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: rus...@winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
signature.asc

andrey mirtchovski

unread,
Apr 6, 2015, 10:53:36 AM4/6/15
to Russel Winder, golang-nuts
> 10.times

10 times what?

Shawn Milochik

unread,
Apr 6, 2015, 10:55:24 AM4/6/15
to golan...@googlegroups.com

On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski <mirtc...@gmail.com> wrote:
>     10.times

10 times what?



Please don't feed the trolls. 

Russel Winder

unread,
Apr 6, 2015, 11:21:50 AM4/6/15
to golan...@googlegroups.com
On Mon, 2015-04-06 at 10:54 -0400, Shawn Milochik wrote:
> On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski <
> mirtc...@gmail.com>
> wrote:
>
> > > 10.times
> >
> > 10 times what?

Rob's code fragment was:

for i := 0, i < 10; i++

without a code body, which was nonetheless implied. I just used the
same sort of fragment showing the control without the following code
block.

> >
> Please don't feed the trolls.

Far from being a troll.
signature.asc

bep

unread,
Apr 6, 2015, 6:37:56 PM4/6/15
to golan...@googlegroups.com

Troll is a trademark of Norway. I'm from Norway, I can use it ...

I like


    for i := 0, i < 10; i++

It was the first loop construct I learnt. And it still makes sense.

bep


mandag 6. april 2015 17.21.50 UTC+2 skrev Russel Winder følgende:
On Mon, 2015-04-06 at 10:54 -0400, Shawn Milochik wrote:
> On Mon, Apr 6, 2015 at 10:53 AM, andrey mirtchovski <
> mirtc...@gmail.com>
> wrote:
>
> > >     10.times
> >
> > 10 times what?

Rob's code fragment was:

        for i := 0, i < 10; i++

without a code body, which was nonetheless implied. I just used the
same sort of fragment showing the control without the following code
block.

> >
> Please don't feed the trolls.

Far from being a troll.

--
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russ...@ekiga.net

Andy Balholm

unread,
Apr 7, 2015, 12:08:31 PM4/7/15
to bep, golan...@googlegroups.com
My favorite construct for “do this a certain number of times but don’t use the counter for anything else" is REPEAT in Logo:

REPEAT 4 [ FORWARD 100 RIGHT 90 ]

But I can’t even remember how to do a loop in Logo where you do use the counter…

Mark J. Reed

unread,
Apr 7, 2015, 12:32:55 PM4/7/15
to Andy Balholm, bep, golang-nuts
Logo makes the iteration count available in the loop body as # whether you want it or not, whether it's a REPEAT loop or a FOREACH loop (in which the current item is also available as ?).  If it is an actual REPEAT loop, then REPCOUNT also returns the same value as the innermost # (in any other context, REPCOUNT returns -1).

But we're talking about Go without the Lo-. For what it's worth, I had no intention of reopening the debate on how to do counting loops; I was just asking if s "don't-care" indicator on the lcv was reasonable, and the answer seems to be "no", so I'll stick to the basic `for i` form.

If I were inclined to add anything to the language for more explicit support of a pure-counting loop, it would probably be to make "range" work on integers, generating a loop from 0 to N-1 for value N.  But that would only handle one common case and I agree that the ensuing worm-can-opening would not be worth it.







--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/G7Cm6wRrVkA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

fatdo...@gmail.com

unread,
Apr 7, 2015, 2:58:33 PM4/7/15
to golan...@googlegroups.com, anl...@gmail.com
i thought the same, but then I thought the answer was so simple that I must be misunderstanding the question.

Haddock

unread,
Apr 7, 2015, 3:50:55 PM4/7/15
to golan...@googlegroups.com


Am Montag, 6. April 2015 16:44:41 UTC+2 schrieb Russel Winder:
On Sun, 2015-04-05 at 14:01 -0700, Rob Pike wrote:
> for i := 0; i < 10; i++
>
> Short, simple, straightforward, clear, obvious, understandable, not a
> puzzle, not cute, not confusing, just right, just do it.
>

Whilst this is the way it has to be done in Go, I think you are wrong
on all counts. This is why things like:

    10.times

    for _ in 1..10

and other variants have been chosen in other languages.

+1
Reply all
Reply to author
Forward
0 new messages