aksharvarma, Evolution of how I think of #loops while #coding:
- When I first learned "loops":
while (condition is true) {do these things, adjust things so a slightly new condition is checked}
// That's where I first saw infinite loop and how there are intentional infinite loops.
- A small step to move condition update out of the loop body:
for (i=0; i< N; i++) {do these things}
// After the couple of days it took to get used to them, I found them neater and closer to how I think of things.
- Most of the time, the i from before is indexing into something, so let's directly deal with the item being indexed:
for item in collection:
do stuffAfter the few days to rewire syntax muscle memory, going back would decidedly feel like a step back.
I don't want to give up automatic (and transparent) out-of-bound checks.
- There are actually only about 3/4 things one does inside a loop:
map/fold/scan/filter function-to-call collection-to-traverse-through
;; Getting rid of explicit indexing was just step one.
-- After a few days/months/years, I now realize that it is more important and less buggy if I think only of the function to call (and whether I want to end up with a new (maybe pruned) collection, a single thing, or "both" (that's how I think of scans))
Alternatively, my evolution as I learned new #programming languages idioms:
#KandR -->
#cpp or #java -->
#python -->
#lisp or #haskell --> ???