5

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.

Comments
  • 0
    For a while I thought I was browsing joke/meme category
  • 0
    @electrineer It’s more of a haiku, I guess.
  • 1
    @Lensflare you didn't see what I did there
  • 1
    @electrineer you got me 😅
  • 1
    If you don't like procedural loops - just go functional: Filter, map and fold often fit even better than foreach.
  • 1
    @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 😟
  • 0
    @Lensflare How do you get away without needing an index at times? I do a lot of gui work and invariably something needs an index.
  • 0
    @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.
  • 1
    @Lensflare I have looked at all the <algorithm> in C++ and did not see good way to get an index that weren't hacky using static int or something. Are you using JS? Is there way to do this in JS?

    Another issue is the legacy interfaces we use have counts for length of data and a routine to return the object which requires an index. So some of this is API issues.
  • 1
    @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.
  • 0
    @Lensflare Another issue I sometimes get is lists of data in separate structures. I want the Python "zip" function to generate a tuple list to iterate over. That would be really useful.
  • 2
    @Demolishun Why don’t you build your own zip function? ^^
  • 0
    @Lensflare I should probably learn how to do this at least. I did another search and found a way to get an index as well. I don't know why I didn't see this method before.
  • 0
    @Demolishun thats clever ^^
  • 1
    @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)
Add Comment