4

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
    @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.
  • 0
    @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.
  • 1
    @Demolishun Why don’t you build your own zip function? ^^
  • 0
    @Demolishun thats clever ^^
  • 0
    @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