6 ms·
Though different people consider different things simple. To one a loop is fine, to another a map is a better choice.
by bufferoverflow 7y ago
Though different people consider different things simple. To one a loop is fine, to another a map is a better choice.
- WalterBright 7y agomap/filter/reduce is simpler, but it takes some getting used to. Loops have worn a deep rut in my brain.
- akavi 7y agoAs someone who was introduced to programming via map/filter/reduce, for loops are incredibly more complicated. I don’t think I’ve ever written one correctly on first try.
- whynotminot 7y agoAre you primarily a JS/front-end dev? Because this is crazy to me! Some languages only use for loops (Go).
- akavi 7y agoFull-stack, spent most of my time on the backend in ruby or scala.
- naasking 7y agoUnfortunately, loops still seem to be the easiest way to iterate over two collections simultaneously.
- akavi 7y ago“#zip”? Or maybe I’m misunderstanding the use case?
- naasking 7y agoWhich only works if the sequences align perfectly. Handling misaligned collections is more awkward with functional constructs. Functional graph programming is also still a bit of an open problem. There are awkward scenarios in both cases.
- akavi 7y agoAh, I see what you mean. Yeah, that’s always awkward Not sure how you deal with that with for loops either. Increment the iteration var in the body of the loop? (Seems scary to me, but like I said, I’ve got terrible intuition with them)
- naasking 7y agoFor something like iterators: var ie1 = foo.GetEnumerator(); var ie2 = bar.GetEnumerator(); while(true) { var has1 = ie1.MoveNext(); var has2 = ie2.MoveNext(); if (!has1 && !has2) break; if (has1) // do something with ie1.Current if (has2) // do something with ie2.Current }
- Gene_Parmesan 7y ago> I don’t think I’ve ever written one correctly on first try. Please don't take offense, but this is odd to me. I honestly severely doubt I am some sort of programming super genius, but I have never had any issues setting looping logic correctly. (I took four years to teach myself programming & CS and now I've been at my first professional dev job for ~6 months.) None of my colleagues seem to have such issues either. What are you experiencing trouble with most? Off-by-one?
- akavi 7y agoI mean, I've written at most a couple dozen in my 7 years as a professional programmer (usually tight loops for perf), so sheer unfamiliarity is a big factor. But yeah, syntax (what order the arguments go in) and off-by-one issues are the majority, I think. Plus figuring out what my initial accumulator needs to be. Idk, map/filter and friends are just a much more direct mapping of how I think of programming.
- prirun 7y agoFor those who may have trouble with loops, take heart: after 45 years of non-stop programming, often 10+ hours a day, I still find myself sometimes doing mental loop simulations with small (few element) data to make sure a loop is correct. It's usually much easier to take extra time to make sure it's right than to debug it later.
- WalterBright 7y agoI've used loops so much I don't even see the loop anymore as a collection of constructs, I see it as a single thing. There are a lot of easy mistakes to make with loops, but I don't make them anymore (of course, by writing that, I will make one!). For example: #include <stdbool.h> #include <stdio.h> typedef long T; bool find(T *array, size_t dim, T t) { int i; for (i = 0; i <= dim; i++); { int v = array[i]; if (v == t) return true; } } There are 5 errors in that example.
- taejo 7y agoIs the fifth error that there are only four errors?
- dahart 7y ago> to another a map is a better choice. Are you thinking more of JS or another functional scripting language than C/C++? C doesn’t have map, so it’s not an option, and in C++ it’s called something else. In JS, I so wish that I could switch to functional constructs like map permanently, but map and foreach are much slower than loops, an order of magnitude or more for tight loops. I’m still forced to use loops in performance critical code, even if I consider map a better choice. Coming back to C++ after having been in JS land for 5 years, C++ feels constantly difficult to use, and all the names for the functional primitives don’t seem to make intuitive sense like they do in JS.
- Gene_Parmesan 7y ago> order of magnitude or more for tight loops I just learned this about JS's map/foreach a few weeks ago. I didn't think it was possible to be more disappointed by JS than I already was, but somehow I managed it.