Re: [go-nuts] evaluation of range expression in for-range loop

2024-02-05 Thread Tamás Gulácsi
Yes. If you manipulate the meaning of the index (i) or the length of the slice, then do it explicitly: for i := 0; i < len(a); i++ { if a[i] == 0 { //delete a[i]=a[0] a = a[1:] i-- } } For other methods, see https://go.dev/wiki/SliceTricks#filtering-without-allocating Shivansh

Re: [go-nuts] evaluation of range expression in for-range loop

2024-02-05 Thread Shivansh Rustagi
Hi Guys, I had a question regarding: > On the other hand, if you have `for i, v := range a {` then the > expression *is* evaluated before the start of the loop. I have a code (https://goplay.tools/snippet/dMwD0_rEhkB) where I am ranging over a slice, and updating (removing the zeroth element for

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Marvin Renich
[Sorry, I accidentally set the Reply-To incorrectly in my previous msg; corrected here. I don't need a CC in responses.] * Jan Mercl <0xj...@gmail.com> [220318 12:04]: > On Fri, Mar 18, 2022 at 4:53 PM 'Axel Wagner' via golang-nuts > wrote: > > I don't really know the answer to your question. I

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jan Mercl
On Fri, Mar 18, 2022 at 4:53 PM 'Axel Wagner' via golang-nuts wrote: > > One thing to keep in mind is that Go development was incremental and was > done, from early on, by having multiple implementations. Incremental > processes often lead to different results than designing a language from a >

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Axel Wagner' via golang-nuts
One thing to keep in mind is that Go development was incremental and was done, from early on, by having multiple implementations. Incremental processes often lead to different results than designing a language from a blank slate. Ambiguities in the spec are often discovered only when someone notice

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Marvin Renich
* 'Axel Wagner' via golang-nuts [220318 08:04]: > https://go.dev/ref/spec#Length_and_capacity says: > > > The expression len(s) is constant if s is a string constant. The > > expressions len(s) and cap(s) are constants if the type of s is an array or > > pointer to an array and the expression s d

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jochen Voss
Thanks Axel, Now this makes sense to me. All the best, Jochen On Friday, 18 March 2022 at 12:04:04 UTC axel.wa...@googlemail.com wrote: > https://go.dev/ref/spec#Length_and_capacity says: > >> The expression len(s) is constant if s is a string constant. The >> expressions len(s) and cap(s) are

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Axel Wagner' via golang-nuts
https://go.dev/ref/spec#Length_and_capacity says: > The expression len(s) is constant if s is a string constant. The > expressions len(s) and cap(s) are constants if the type of s is an array or > pointer to an array and the expression s does not contain channel receives > or (non-constant) functi

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread 'Dan Kortschak' via golang-nuts
On Fri, 2022-03-18 at 04:43 -0700, Jochen Voss wrote: > Dear all, > > The spec at https://go.dev/ref/spec#For_range says > > "The range expression x is evaluated once before beginning the loop, > with one exception: if at most one iteration variable is present > and len(x) is constant, the range ex

Re: [go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jan Mercl
On Fri, Mar 18, 2022 at 12:43 PM Jochen Voss wrote: > The spec at https://go.dev/ref/spec#For_range says > > "The range expression x is evaluated once before beginning the loop, with one > exception: if at most one iteration variable is present and len(x) is > constant, the range expression is n

[go-nuts] evaluation of range expression in for-range loop

2022-03-18 Thread Jochen Voss
Dear all, The spec at https://go.dev/ref/spec#For_range says "The range expression x is evaluated once before beginning the loop, with one exception: if at most one iteration variable is present and len(x) is constant, the range expression is not evaluated." What does the second half of this