Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
If you don't like procedural loops - just go functional: Filter, map and fold often fit even better than foreach.
-
@Oktokolo yup. I use those probably 99% of the time. But when I use the procedural loops, it’s 90% forEach.
I’ve read there are some secure, turing incomplete languages that provide some compile time guaranties about correctness by sacrificing the unbounded while and for loops.
Maybe I’m subconsciously preparing myself to use those languages in the future 😟 -
@Demolishun I don’t really get what you mean or what this has to do with looping/iterating.
I almost never need an index because I get the elements of the collection directly when iterating.
And even when I need an index, I can use the same forEach and functional style loops to get it. -
@Demolishun I’m using Swift and everything that can be used in a loop (iteratable) can be mapped to a collection of (index,value) tuples. So, I get the indexes and the elements.
If I have just the count, I can simply create a range from 0 to the count and iterate over that range. Than use the index to access the value in each iteration.
All with forEach.
I’m sure that it would be possible to build functions to do that in C++ and JS. But I agree that it is more common to work with classic for loops and indexes there. -
@Demolishun I’d probably make a custom function naively:
@highlight
extension Collection {
func enumerate() -> [(Int, Element)] {
var index = 0
return self.map { element in
let result = (index, element)
index += 1
return result
}
}
}
(Swift syntax)
Related Rants
While while is still useful sometimes, all hope is lost for for.
For each for each that I use, I love it more and more.
random
loops