Tales from the jar side: Kotlin decrement with modulus, Happy birthday to my car, and Notes on popular media
Too much sex hurts your memory. I read that in a medical journal dated November 17, 2007, on page 34. It was a Saturday and I'd just finished a breakfast of eggs and bacon.
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of August 14 - 21, 2022. This week I taught a Reactive Spring course on the O’Reilly Learning Platform, and two days of a private Android course.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, when this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile.
Kotlin Modulus Operator Method
In my Android course this week, I had learned about a simple method that was added to Kotlin the last couple of versions. The problem to solve was really simple. It comes from an exercise in the book Android Programming: The Big Nerd Ranch Guide (5th edition) where a few of the chapters involve building a quiz app with a series of questions. Here’s a portion of the user interface:
One of the challenges is to add a Previous
button similar to the Next
button. The questions are saved in an array, and the app keeps track of which question you’re on using an integer called currentIndex
. In Kotlin, pressing the Next
button triggers a function called moveToNext
:
The index is incremented, modulo the size of the question array, using the remainder operator, %
. The questions therefore wrap around. If there are three questions, the sequence of index values is 0, 1, 2, 0, 1, 2, and so on.
The challenge is, how do you implement the similar moveToPrevious
method? How do you get the correct numbers in descending order?
If I use the same calculation but just subtract 1 instead of adding 1:
currentIndex = (currentIndex - 1) % questionBank.size
That doesn’t work. The resulting index values immediately run into problems. Starting at 0, the sequence of index values is 0, -1, -2, 0, -1, -2, and you throw an ArrayIndexOutOfBoundsException
on the first negative index.
My quick-and-dirty solution was to check for -1 and then reset the counter:
That’s easy enough, and it works, but I kept thinking there ought to be a better way.
One alternative is to just add the the list size (minus one) to the currentIndex
and then do the modulus:
That works, but looks pretty weird to me, even though I think I get it. I mean, where am I decrementing the index? I’m not, directly, but I am implicitly via the modulus operator. Feels strange.
It turns out that Java has a method that does the trick, called, of all things, floorMod
:
The Javadocs for floorMod say that the “floor” part means to round down. I would explain how this method works, but I’m still confused by it, even after reading the docs.
It turns out, however, that Kotlin added a method in version 1.5 that does the same thing, but much more cleanly. In a section of the release notes called Floored division and the mod operator, it says “mod()
returns the remainder of floored division (modulus).” The resulting code should therefore be:
That has a beautiful symmetry to the moveToNext
implementation. Kotlin has operator overloading, so the %
operator already delegates to a function called rem
(as in “remainder”). Now, for potentially negative values, I know to use mod
instead.
Happy Birthday, Nick
On April 1, 2016, I happened to be in Boston recording a video for the O’Reilly Learning Platform. That was the first day you could order the Tesla Model 3, which was announced but not yet available. The formal announcement was scheduled for 7pm Pacific Time, and online orders could proceed after that, but if you could make it to an actual Tesla store, you could order your car any time that day.
I found a Tesla store after our recordings were done, which meant I was behind all the people who stood in line in the morning, but before all the online orders. The store was still relatively full when I got there, but I didn’t have to wait in line. I put down my $1K deposit and prepared to wait the expected two years (!).
My number came up in April of 2018, but the car they offered me had only rear-wheel drive and I wanted the all-wheel version. After all, I don’t live in Southern California. I live in Connecticut, where the weather is your enemy most of the time. I therefore waited and expected to hear back in October.
I received the email telling me I could get my car in August. There were no Tesla dealers in Connecticut (still true to this day — the dealer’s lobby managed to keep them out), so we had to go to New York to pick it up, but I received my blue Tesla Model 3 on August 23, 2018. On Tuesday it will be four years old.
According to my app, the car currently has 23,102 miles on it. Round that up to 24K, and I’m putting on just under 6000 miles a year. You can thank the pandemic for that, as well as working from home.
For the record, I still love it. I leave it plugged in the garage all the time (as they recommend). I’ve had two service visits, and on one of them they came to me for a factory upgrade. I’ve visited superchargers about a half a dozen times, and never spent more than $10 on any of them. I still have about a 250 mile range, which turns out to be way more than I need for almost everything I do. The software updates automatically on a regular basis.
We have an expensive full-service gas station in town. I’m always tempted to pull up, wait for the attendant to come over, and say, “No thanks, I’m good,” before driving away, but that’s more fun to think about than to actually do.
I have a much lower regard for Elon Musk now than I had then, but I still like the car and would recommend it to anyone.
When you set up your new car, you have to give it a name. Mine is called Nick, for Nikola Tesla, of course.
Random Tweets
Job Opening
The Breaking Bad prequel Better Call Saul ended this week. I thought the series started off slow, especially compared to how Breaking Bad ended, but it got much better as it went along. The last half-dozen or so episodes were wonderful, and though the finale ended more quietly than I expected, the more I think about it, the better it worked.
(In case you don’t get the reference in the tweet, in the final season, Jimmy McGill aka Saul Goodman, Attorney at Law (i.e., (it)’s all good, man), was hiding out from his many enemies (including both the Mexican drug cartel and the Federal Government) by taking the role of Gene Takovic, manager of a Cinnabon in Omaha, Nebraska. After being arrested at long last, he used his one phone call to let them know they needed a replacement.)
Great Costume … Or IS IT?
The Prey movie (on Hulu) is a new entry in the Predator series, most of which I’ve skipped. It features an early predator landing in the northern plains, among the Comanche nation, in 1719. The predator is ultimately defeated by a very impressive young girl named Naru, played by Amber Midthunder, who does a brilliant job. (Her brother Taabe, played by Dakota Beavers, is also excellent). The movie is way better than you expect from that franchise. My wife and I both enjoyed it
Ring of Power, For Sale, Cheap
Galadriel is cool and all, but you have to admit her vocabulary is a bit on the flowery side. A few examples:
Gal: They were all of them deceived.
Translation: He lied.
Or
Frodo: What will I see?
Gal: Even the wisest cannot tell. For the mirror shows many things. Things that were, things that are, and some things... that have not yet come to pass.
Translation: Plot stuff, before and after.
Or even
Frodo: If you ask it of me, I will give you the One Ring.
Gal: You offer it to me freely? I do not deny that my heart has greatly desired this... [Suddenly fierce] In place of a Dark Lord, you would have a Queen! Not dark, but beautiful and terrible as the Dawn! Treacherous as the Sea! Stronger than the foundations of the Earth! All shall love me and despair! [Stops, shaken, and becomes calm again] I pass the test. I will diminish, and go into the West... and remain Galadriel.
Translation: Yeah, no.
Mishmashed Memes
I was going to include this from a tweet, but I thought I’d transcribe it instead.
To the tune of The Wellerman, as sung by Captain Picard:
There once was a ship twas on TV
The name of the ship was the Enterprise D
A cube drew up, the shields went down
Oh blow those Borgy boys blow(Chorus)
Soon may the yeoman come
To bring us Earl Grey tea and rum
One day when the battle is won
We’ll make it boldy so
I had to include the original mashup:
If you’ve got about two minutes to listen, that video has — seriously — over 20 million views.
Ain’t Nothing But A Heartbreak
“I want it that way” works too.
(Good luck getting that ear worm out of your head.)
Finally, Acceptance
(Presumably Chaz in this tweet is Charles Addams, creator of the Addams Family cartoons for over 50 years starting in 1938.)
May you find someone in your life who looks at you the way Gomez looks at Morticia when she speaks French.
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Reactive Spring and Spring Boot, on the O’Reilly Learning Platform
Android development, private class
This week:
Getting Started with Spring and Spring Boot, on the O’Reilly Learning Platform (APAC time zone)
Deep Dive: Spring and Spring Boot, an NFJS Virtual Workshop
Android development, private class