Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-23 Thread C Banning
Well, I imagine there's only a few types in any code where the logic would require retrieving the "last" member of a list or array. Something like the following could address that and be more performant than using reflection - https://play.golang.org/p/XTbuf7RegF2. On Sunday, June 14, 2020 at

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-22 Thread Jesper Louis Andersen
On Sat, Jun 13, 2020 at 4:42 PM Tom Limoncelli wrote: > I've been coding for a few years (first line of BASIC was in September > 1979) and I have to admit that off-by-one errors have always been a > problem for me. I guess I'm just that kind of sloppy. > > They are surprisingly common! It has

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-14 Thread Jake Montgomery
On Saturday, June 13, 2020 at 1:09:16 PM UTC-4, Jan Mercl wrote: > > On Sat, Jun 13, 2020 at 5:37 PM Jake Montgomery > wrote: > > > You cant really write a short 'end()' function. You have to write an end > function for every type of slice. Or use reflection, which would make the > function

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-14 Thread Tom Limoncelli
Thank you for the feedback! I took Jan Mercl's suggestion and wrote the short function to see if it is useful. It is based on C Banning's suggested code. I'm going to start using it on my projects to see if it helps. The module is called "hind":https://github.com/TomOnTime/hind hind.S(x)

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-13 Thread Jan Mercl
On Sat, Jun 13, 2020 at 5:37 PM Jake Montgomery wrote: > You cant really write a short 'end()' function. You have to write an end > function for every type of slice. Or use reflection, which would make the > function slow, and not inlined. Generics anyone? I haven't mentioned anything about

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-13 Thread C Banning
https://play.golang.org/p/EZEXmBig1y- On Saturday, June 13, 2020 at 9:37:35 AM UTC-6, Jake Montgomery wrote: > > > > On Saturday, June 13, 2020 at 10:55:43 AM UTC-4, Jan Mercl wrote: >> >> On Sat, Jun 13, 2020 at 4:42 PM Tom Limoncelli >> wrote: >> >> I'd suggest to just write the short 'end'

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-13 Thread Jake Montgomery
On Saturday, June 13, 2020 at 10:55:43 AM UTC-4, Jan Mercl wrote: > > On Sat, Jun 13, 2020 at 4:42 PM Tom Limoncelli > wrote: > > I'd suggest to just write the short 'end' function by yourself if you > think it's useful for you. It does not harm performance as it will get > inlined AFAICT.

Re: [go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-13 Thread Jan Mercl
On Sat, Jun 13, 2020 at 4:42 PM Tom Limoncelli wrote: I'd suggest to just write the short 'end' function by yourself if you think it's useful for you. It does not harm performance as it will get inlined AFAICT. But it IMO hurts readability a bit. I'd prefer to read the explicit `len(x)-1`

[go-nuts] Go has eliminated most of my off-by-one errors except 0... I mean 1.

2020-06-13 Thread Tom Limoncelli
tl;dr: Go's "range" operator has eliminated the most common trap where I make off-by-one errors. The next largest category of off-by-one errors would be eliminated if there was a way to specify the last item in an array. It would also improve a developer's ability to convey intent. ... I've