4 ms·
The syntax I always want but no language I know of supports: for 1 <= i <= 10: do stuff for 0 <= i < 10: do stuff for 1 <= i < j
by gjm11 7y ago
The syntax I always want but no language I know of supports:
for 1 <= i <= 10:
do stuff
for 0 <= i < 10:
do stuff
for 1 <= i < j <= 10:
do stuff
(In the last case, the exact order in which the 45 iterations happen should maybe be left unspecified; at any rate, it would be bad style to depend on it.)
Downward iteration:
for 10 >= i >= 1:
do stuff
Obviously this doesn't cover every case of "arithmetic for loop" that would be useful: sometimes you want a stepsize that's neither 0 nor 1. I'd be quite happy with a language in which I had to do that using a more general iterate-over-an-arbitrary-sequence construction; I'm tempted by options like
for 100 <= i <= 1000 where i%3==1:
do stuff
but it's probably too clever by half; either you only support the simple "specify the value of the variable mod something that doesn't change while iterating" case, in which case users will complain that some obvious generalizations fail to work, or you support arbitrary predicates, in which case you have to choose between making the "easy" cases efficient and the "hard" cases not (in which case users will be confused when what seem like small changes have drastic effects on the runtime of their code) and making all of them inefficient (in which case users will be confused by how slowly their code runs in some cases).
- arh68 7y agoYou may enjoy Common LISP's looping: it's a whole minilanguage where you can say stuff like (loop for i from 1 upto 10 ..) (loop for i from 1 below 11 ..) (loop for i from 10 downto 1 ..) (loop for i from 10 above 0 ..) (loop for i from 3.5 to 7.5 by 0.25 ..) It does much more than that, I should mention; it's a whole mini-language/DSL (like format).
- kazinator 7y agoPlus loop has unspecified behaviors leading to nonportable code that OP might enjoy.
- gjm11 7y agoNot only may but do; I like Common Lisp a lot, and while some people complain that LOOP isn't lispy enough I personally do like it. But it doesn't have the elegance of just specifying the inequalities you want your loop variables to satisfy :-).
- kazinator 7y ago> (In the last case, the exact order in which the 45 iterations happen should maybe be left unspecified; at any rate, it would be bad style to depend on it.) Relying on something unspecified isn't just bad style; it's a bug. Why would you introduce requirements into a higher level language that leave important aspects unspecified, opening the door to bugs?
- gjm11 7y agoSorry, I wasn't clear enough. I meant: 1. It might be best not to specify what happens, because 2. if it were specified then people would rely on the specific order, which would be bad style because it would make code harder to read and be more liable to errors if anyone misremembers what order the iteration happens in. (But it's true that people might rely on the observable behaviour even if it weren't officially specified, and that would be even worse. Which is why I said "should maybe be left unspecified"; it isn't obvious to me which way is better overall. If it's unspecified then fewer people will rely on it but the consequences each time will be worse.)