6 ms·
C-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
by proto_lambda 4y ago
C-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.