6 ms·
”Bad idea. Great way to get off-by-one errors.” I would think that depends on whether that gets taught at schools early in life. Intervals that are closed at
by Someone 7y ago
”Bad idea. Great way to get off-by-one errors.”
I would think that depends on whether that gets taught at schools early in life.
Intervals that are closed at the low and open at the high end also are convenient when using zero-based indexing (you want an easy way to express 0,1,2,…,n-1, and if you’re used to the notation [0,n) is nicer than [0,n-1])
I would think overloading the meaning of the various parentheses makes parsing and generating clear error messages harder, though (but not as hard as when one would follow ISO 31-11 and allow such things a ]3,7] for an half-open interval excluding 3, but including 7)
I think the desire to not overload parentheses with yet another meaning is why ‘Modern’ languages tend to use an infix operator for ranges, e.g. ‘1 to 5’ or ‘1...5’, with Swift having the half-open variant ‘1..<5’ (IMO ugly, but clear, so I guess one would get used to it).
- a1369209993 7y ago> you want an easy way to express 0,1,2,...,n-1 I'm partial to the viciously set-theoretic[0]: for i in 5: print i # 0;1;2;3;4 0: finite ordinals[1] are 0={},1={{}}={0},2={{},{{}}}={0,1},3={{},{{}},{{},{{}}}}={0,1,2},Sx=x∪{x}={0,...,x} 1: aka naturals or nonnegative/unsigned integers
- Zarel 7y agoThat only works if you're ranging from 0 to n-1, though. A popular way to partition an array at i and j is to do: arr[0..<i], arr[i..<j], arr[j...-1]
- a1369209993 7y agoa0,a1,a2 = arr.splitat({i,j}) maybe? It avoids repeating i and j twice and arr thrice, at least. Admittedly, there probably are cases where you really do want a range [x..^y] for arbitrary x and y, but it seems like most cases aren't.
- FisDugthop 7y agoSwift may have borrowed it from E, where the half-open variant is `1..!5`, read "from 1 up to, but not including, 5". It is extremely useful for 1-to-n sorts of counting tasks.
- WalterBright 7y ago> 'Modern' languages After having many fencepost errors, I finally came to the conclusion that all my code shall henceforth be zero-based. I'm much happier not having those errors anymore. I.e.: for (i = 1; i <= N; ++i) The <= is always a huge red flag for me, and I rewrite it as: for (i = 0; i < N; ++i) and add a `1` in the body if I must.
- chopin 7y agoAny specific reason to use ++i over i++? I always use the latter but only out of habit. I've rarely seen code with the former (your idiom).
- lmm 7y agoIn theory extremely naive compilers may copy `i` if you use `i++` (since `i++` evaluates to the old value, whereas ++i can always be a destructive update), so some programmers have a habit of defaulting to ++i.
- WalterBright 7y agoIt's just better form. Also, if `i` is a struct with an overloaded postinc operator, it can be more expensive.
- jstimpfle 7y agoI think that is arguable. The form "array[idx++] = foo;" has undeniable elegance, and I still use it sometimes, even though I've developped a pretty verbose coding style in general.
- chopin 7y agoYeah, but why is it better form (I am coming from Java where it shouldn't make any difference)?
- WalterBright 7y agoIt's better form to not ask for a characteristic that you don't need. For example: 1. increment x 2. take the value of x, then increment x (1) is better form if you don't need the value of x.