5 ms·
I don’t know why these new languages have to have their own special takes on loops without giving us the classic C99 version of the three-clause-for-loop. Even
by david2ndaccount 4y ago
I don’t know why these new languages have to have their own special takes on loops without giving us the classic C99 version of the three-clause-for-loop. Even JavaScript has it. If they want to improve on it, then allow us to declare variables of different types in the first clause. Yes, using the C-style loop to iterate over a container sucks, but that is an argument for also having a for-each loop. There are lots of low level algorithms that are just better to express with the three-clause-for-loop.
- proto_lambda 4y agoC-style for loops can almost always be replaced with iterators in a more concise manner. No need for the explicit termination check or the explicit advancement expression, that's already handled by the iterator.
- guntherhermann 4y ago> C-style for loops can almost always be replaced with iterators in a more concise manner. Not if you want to optimise for performance. A simple example: I search through a list of n items to find item m, iterators typically don't let you break out of the iteration. Zig is the only contender I've seen where the for loop looks iterator-like, but also supports break/continue.
- proto_lambda 4y ago> A simple example: I search through a list of n items to find item m, iterators typically don't let you break out of the iteration. That'd be a pretty crappy iterator implementation, short-circuiting is basically what iterators are all about (how else would you handle infinite iterators?). For example, in Rust, it's simply Iterator::find(): https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.find https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho... for loops in Rust are basically just syntax sugar for a while loop taking an element of the iterator each iteration, you can use break/continue as normal.
- FromTheSteppe 4y agoSome languages support early return(break) and labeled return (continue) in lambdas. Although good languages have lazy iterators or optimizing compilers. One would use iterator.takeWhile(x => x is not m) in your case, or filter, and skip in other cases. Better languages even allow you to yield return and write your own lazy iterator transformations.
- kaba0 4y agoIs there any language that won’t allow breaking out from an iteration? I can only imagine some FL language where the default loop construct would disallow that, but even there it is quite common to just return early syntactic sugar.
- mekoka 4y ago> iterators typically don't let you break out of the iteration. Zig is the only contender I've seen where the for loop looks iterator-like, but also supports break/continue. I'm curious as to which language you worked with that didn't allow this.
- anecdotal1 4y agoDlang isn't a contender here? It can do most everything Zig can do, speaks C natively, and it supports break/continue? edit: plus has a GC if you don't want to do manual memory management, already has a package manager, good at producing native/static binaries, can do automatic type inference, user defined types, cross compiling is super easy...
- Tozen 4y agoYou made a good point, that is often overlooked about memory management, as if all users will always want to do it manually. The reality is, many don't want to deal with it at all or only for specific projects and reasons. For new and various users, not having to worry about manual memory management until they are ready or want to, can be a huge relief or increase productivity.
- nsajko 4y agoIterators are often nice, but are no replacement for loops. Try processing multidimensional arrays without loops.
- firstlink 4y ago> iterators > without loops This is a false dichotomy. You can use iterators to replace three-clause loops without changing the body: for row in 0..rows { for col in 0..cols { arr[row][col] } } This is far easier to read than three-clause loops which repeat redundant information repeatedly and redundantly.
- nsajko 4y agoOK, but you're still using loops.
- proto_lambda 4y agoYou are, but that was never in question.
- pornel 4y agoBecause the classic for loop is prone to off-by-one errors, and it's unnecessarily boilerplatey for simple cases. OTOH complex cases that take advantage of customizing the three clauses or independently modify iteration variable(s) are usually "clever", not in a good way.
- giancarlostoro 4y agoYou can still have it but add more modern syntax for looping. Hell Java has "goto" reserved if I remember correctly.
- pornel 4y agoAll languages with a simplified `for` loop have kept `while` for the other cases. That makes the old `for` just a redundant syntax.
- david2ndaccount 4y agoZig supports the 2nd and 3rd clause already in its while loop. Would it kill it to accept an initializer and look more like other languages instead of having a special snowflake syntax. Is this (note the enclosing braces) { var thing = thinger(); while(cond(thing)) : (mutate(thing)){ // do whatever } } really better than this? for(var thing = thinger(); cond(thing); mutate(thing)){ // do whatever }
- Tozen 4y agoVlang, is a newer programming language (with various modern features, including package manager) that also has the classic C style for loops. https://github.com/vlang/v/blob/master/doc/docs.md#c-for https://github.com/vlang/v/blob/master/doc/docs.md#c-for