Scala, Java, Unix, MacOS tutorials (page 1)

After my previous post about electing people who don’t believe in science, I came across this post about having a “peeing section” in a swimming pool that sums up my feelings about not having a nationwide lockdown.

We are, after all, “One nation under God.”

Not having a nationwide lockdown, and a “peeing section” in a swimming pool

I don’t even know who the My Pillow guy is, but when I saw this image I thought, the United States picked a horrible time to elect people who don’t believe in science.

That the entire nation isn’t under lockdown at the same time is one of the dumbest things I can imagine at a time like this. Not locking everyone down means the coronavirus is going to go on longer, and more people will get sick and die.

The U.S. picked a horrible time to elect people who don’t believe in science

I never met Carl Quinn in person, but I knew of his work. I just read that he passed away from the Coronavirus/Covid-19.

Carl Quinn, software developer, passes away from the Coronavirus

The following is a definition of equanimity, and how equanimity differs from indifference or apathy. The entire quote comes from Shinzen Young:

“Many people get confused about what equanimity is and what it isn’t. Here are some examples that may help clarify.

Equanimity can be deepened by relaxing tension around pain, such as the pain of a broken leg. But equanimity does not mean that you don’t care about getting proper medical attention, such as getting a cast.

Equanimity means opening up to angry sensations that may arise during an argument. But equanimity does not mean that you should stay in a bad relationship.

Ugh, afternoon naps are the worst. I was feeling pretty out of it today, so I decided to lay down, and quickly fell asleep.

When I wake up I hear some children playing outside. I listen to them talk and laugh for a little while, and a thought comes to me that there are more children living here than there used to be. But I like hearing their voices; I'm glad it’s sunny out, and that they’re happy and playing.

The nap seems to be over, so I try to get out of bed — but I can’t move anything. Something is wrong. Very wrong. I’m groggy, and feel extremely heavy. My left arm won’t move. I panic. A woman comes over and I ask her to help me, reaching up weakly with my right arm. I black out or fall back to sleep.

I wake up again, but as I try to move I realize I still can’t move much. My head moves, maybe my right arm, too. Have I been drugged? Am I paralyzed? This time I’m determined to get up, and then manage to fall out of bed and onto the floor. I lay there, face down, with my left arm pinned under my body. I really can’t move ... what’s going on?

Here’s another view of that bright star that’s due west over the Rocky Mountains, as seen from my tiny apartment in Broomfield, Colorado.

The west star over the Rocky Mountains

A great thing about living in this area of Colorado is that there’s a bright star that shines due west over the Rocky Mountains. Every night I open up the shades so I can see it. The only thing that’s on my bucket list is to spend as much time as possible in the mountains to see all of stars up close.

The star is the white dot in the center and bottom one-third of the image, above the roof. It looks more impressive in person. The big white blob in the upper-left is the Moon.

The bright star due west over the Rocky Mountains

“I wish it need not have happened in my time,” said Frodo.

“So do I,” said Gandalf, “and so do all who live to see such times. But that is not for them to decide. All we have to decide is what to do with the time that is given us.”

“To be a programmer is to develop a carefully managed relationship with error. There’s no getting around it. You either make your accommodations with failure, or the work will become intolerable.”

~ Ellen Ullman (via this tweet)

This quote makes me think of all those years of exception-handling with Java. I never knew there was a better way to handle errors, so I developed a strategy of letting my exceptions bubble up to the controller level (as in model/view/controller), where I would deal with them. These days I know I can use Option/Some/None in Scala, as well as Try/Success/Failure.

I was writing some Scala code like this today:

val sb = new StringBuilder
for (b: Byte <- mdOut.digest) {
    val hexString: String = String.format("%02x", b)
    sb.append(hexString)
}
sb.toString

and encountered this error message:

Under Your Scars by Godsmack is today’s song of the day:

And everything feels broken
When you’re not next to me
Would you still be you
If we weren’t we

Remember all those people having a good time on the beach in Ft. Lauderdale a few weeks ago? According to their cellphones, this is where they went when they left the beach.

~ image comes from this video

Where the spring breakers went after Ft. Lauderdale (Coronavirus 2020)

I can’t find the original source of this image and product, but I have always liked the “Live Brave” saying, which I first heard on the excellent Eli Stone tv series (which stars Johnny Lee Miller, who may be more well known for the movie Hackers and of course, Elementary).

Live Brave bracelet

While this photo looks like a sunset, it was actually a sunrise. I took it in Virginia Beach on April 17, 2017.

Dark sunrise, Virginia Beach

Last week I finished writing some Scala code to convert this website from using Drupal 8 to using static web pages. Technically what happens is that I use Drupal at a different location, and then my Scala code reads the Drupal database and uses to the Twirl template system — that comes with the Play Framework — to convert that data into the static HTML pages you see here.

Along the way I learned a lot about the 2020 version of Twirl templates, so I thought I’d share some tips and examples about how to do things with Twirl. If you ever want to use Twirl in standalone mode like I did, or inside the Play Framework, I hope this is helpful.

If karma is defined as “cause and effect,” here’s a little karma of the last 4-5 months:

  • In early November, 2019, I suddenly started having severe chest pain.
  • I happened to be standing next to my computer, so I quickly disabled comments on alvinalexander.com. In case this was something I couldn’t come back from, I didn’t want other people to have to worry about dealing with those.
  • (At the hospital I would find out this was pericarditis, i.e., inflammation around the heart.)
  • Over time I realized that “no comments” meant less stress and less work, so I kept them turned off.
  • One day I realized that if I was going to keep comments turned off, there was no reason to serve pages dynamically with Drupal 8 any more.
  • I took a week to write some Scala scripts to convert Drupal 8 blog content to static web pages.
  • My server CPU use has dropped significantly, so I can reduce my server costs by about $400/year.

Dr. Foreman: The kid was just taking his calculus exam when all of a sudden he got nauseous and disoriented.

Dr. House: That’s the way calculus presents.

Our sunset on the evening of Thursday, March 26, 2020, had a pastel feel.

Pastel sunset

I think a lot of people in the United States will remember the New York Times front page for March 27, 2020, for a long time.

The New York Times front page, March 27, 2020

As a brief note, here’s a Scala function to get the Unix epoch time for X days ago:

/**
 * Returns a 10-digit Long (like 1585275929) representing the date/time.
 * Use it to get the time for 1 day ago, 2 days ago, etc. `0` will give
 * you the current time.
 */
def unixEpochTimeForNumberOfDaysAgo(numDaysAgo: Int): Long = {
    import java.time._
    val numDaysAgoDateTime: LocalDateTime = LocalDateTime.now().minusDays(numDaysAgo)
    val zdt: ZonedDateTime = numDaysAgoDateTime.atZone(ZoneId.of("America/Denver"))
    val numDaysAgoDateTimeInMillis = zdt.toInstant.toEpochMilli
    val unixEpochTime = numDaysAgoDateTimeInMillis / 1000L
    unixEpochTime
}

As shown in the comments, if you give it a 0 it will return the current epoch time.

Of course you can make the code shorter and better; I just wanted to show the steps in the approach using the Time classes in Java 8.