Modulus-based index for cycling array values

Ah! I forgot about using modulus operator as array index for cycling through array values! Thanks for the reminder, Angular docs (One Time Binding example).

Note the relevant bit:

$scope.name = names[counter % names.length];

Normally I think I’d do something dumb like:

  var counter = 0;
  var names = ['Igor', 'Misko', 'Chirayu', 'Lucas'];

  $scope.clickMe = function(clickEvent) {
    $scope.name = names[counter];
    if (++counter >= names.length ) {counter = 0;}
  };

Which maybe I guess isn’t super dumb, but I like the elegance of the Angular example, as it does away with the minor-but-present cognitive overhead of “do I greater-than/equal this, or just greater than, or pre-increment, post-increment, subtract one from the array length?” mess.

Also, in my version, you lose the counter’s true value, as it becomes more of a cycle counter.

Altered coding

Coding while stoned is such a double edged sword: you will see new approaches to old problems, but you will also go on wild-bug-chases that are rivaled only by some of your earliest/stupidest programming blunders. The “aha!” moment, when it comes, may be ridiculous, so it helps to laugh at it. Coincidentally, certain substances predispose one to laughter.

I don’t think any sort of altered state is a good default for day-to-day problem-solving, but regimented and used strategically and sparingly, it can yield good rewards. As with most things, careful reflection is important to maximize future potential.

some thoughts on language

I should probably learn a second (natural) language. I know the rudiments of a few aside from being a half-way decent native English speaker, but can’t fluently speak any of them, nor understand anything but simple expressions. I feel like such an acquisition would change me as a person in important ways.

It’s fun(ny) to ponder how I’ve spent more time studying designed languages that are interpreted and/or compiled into machine code instructions for CPUs to run, as opposed to learning new ways to communicate to people around me.

The parallels between study of & experience of using a natural language versus the study of & experience using a programming language are certainly there: picking up of bad habits; development of slang, shorthand, idioms, and the like; proclivity to sticking to what you know, e.g., solving a problem using the methods or messages you’re familiar with; merging of elements of one language into another over time, or spin-off dialects; dynamic languages being able to modify themselves (e.g., through “monkey patching” in a language like Ruby) and natural languages being subject to neologism and appropriation of rules for other purposes.

But there are of course differences: there is not often the equivalent of verbacious purple prose when solving a particular programming problem; those that learn and acquire proficiency in programming languages tend to know—or at least have a working knowledge of—several if not a dozen languages, while those that speak natural languages may be bi- or tri-lingual and leave it at that; and of course speaking a natural language to another person may just be chit-chat, random, gratuitous, or even unnecessary, while whilst programming, one usually wants to write as much code to solve a problem or get a desired result, and anything extra is fluff. (A notable exception to this would be the act of “commenting” code: leaving instructions or documentation about why a certain piece of code was written to either the programmer him/herself or to any future inspectors/caretakers/users of the code.)

Welp, just thoughts. I should probably go study some Latin. Or Lisp.