Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

When Mrs. Albert Einstein was asked if she understood her husband’s Theory of Relativity, she replied, “No ... but I know my husband, and he can be trusted.”

I was just reminded of Rubber Duck Debugging. From this Wikipedia link, “The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line-by-line, to the duck.” For me, my rubber duck is Albert Einstein.

Rubber Duck Debugging

Knowing of my interest in Zen, a friend of mine sent me this photo of a letter from Albert Einstein to a parent grieving after the loss of a child:

Albert Einstein, Zen Master

In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:

def getName[A <: RequiredBaseType](a: A) = ???

That example says, “The parameter a has the generic type A, and A must be a subtype of RequiredBaseType.”

Enjoyed Cowboys & Aliens at Wasilla’s fancy new stadium seating movie theater. How strange to walk out of a theater and into a valley of fireweed surrounded by misty mountains.

(A Facebook post from Wasilla, Alaska, August 9, 2011.)

“When you blame others, you give up your power to change.”

~ Possibly said by Robert Anthony first

The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).

Scala for Java devs: Everything in Scala is an object, even numbers

“I have already lost touch with a couple of people I used to be.”

~ Joan Didion (full quote here on Goodreads)

The Tower app website has a good description of git fetch vs pull. “You can never fetch often enough” is a helpful phrase.

Git fetch vs pull

After yesterday’s Scala nested Option + flatMap/for example, here’s another example of plowing through nested Options with flatMap. First, start with some nested options:

val o1 = Option(1)
val oo1 = Option(o1)
val ooo1 = Option(oo1)

Here are those same three lines, with the data type for each instance shown in the comments:

Bill Wyman, bass guitarist for the Rolling Stones, ranks all 165 Pink Floyd songs from worst to best.

Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one Option instance contains one or more other Options, this article may be helpful.

There are times when you’re creating your domain model when it makes sense to use optional fields in your case classes. For instance, when you model an Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:

We were playing at our camp when my older brother — who was standing on higher ground than I — saw something in the distance. He stood upright, then perfectly still. After a few moments he turned to me in a look of panic I had never seen before, pointed in a direction opposite from where he was looking, and screamed, “Run! Run!” I was startled at his behavior but I knew that something was very wrong, so I ran. And I ran.

I ran as fast as I could, weaving through the brush and constantly changing my course as I was chased by a white man on a dark horse. I thought I might be close to safety when I darted through some bushes, but I ran right into a creek that was too wide to jump across. As I paused for a moment to decide how to continue, the white man shot me in the back.

In intense pain and sudden shock, I stumbled forward into the creek, bent over with one hand in the creek. As I attempted to stand up and regain my balance, I was shot in the back again. This time my body flew forward towards the opposite side of the creek. I tried to control my fall but could not, and my torso slammed against the land. The right side of my face was pressed against the ground, my eyes still open. My right arm was trapped under my body, my left arm was somewhere down my left side. My legs lay in the creek’s water.

Here’s a Facebook post on how using AI has improved their language translation system by 11%.

“Between stimulus and response, there is a space. In that space is our power to choose our response. In our response lies our growth and our freedom.”

~ Viktor Frankl (author of Man’s Search for Meaning)

Table of Contents1 - String equality2 - Multiline strings3 - String interpolation/substitution4 - Substrings5 - Treating a String as an Array[Char]6 - Using map and for7 - Regular expressions8 - Transforming arrays to a String9 - A lot of String method examples

This page contains a collection of over 100 Scala String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)

First, here are some basic uses of the Scala String class to help get us warmed up:

I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.

In general, you probably won’t want to run database queries using a Thread, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:

I woke up on the left side of a king size bed this morning. It wasn’t my bed, but I was blanketed in a thick, soft comforter, which felt wonderful. I looked around briefly. Wherever I was, the room seemed very nice. It was light outside.

As my other senses came to me, I realized that what woke me up was a strange woman standing on the right side of the bed, yelling at me. She wanted to know where I got the warm Cinnabon that was on the nightstand next to me. More accurately, she rather forcefully wanted to know if someone brought that for me, and if so, who.

When I watched the movie Coffee Shop, I knew that Laura Vandervoort wasn’t singing the songs in the movie, and I assumed that the artist singing was Norah Jones, but it turns out that was wrong. The actual singer’s name is Mandi Mapes, and the two songs are called “Dance With Me” and “In Your Arms.” Videos for both of the Coffee Shop songs are shown below.

“Pure functional programming is programming with mathematical functions.”

~ Erik Meijer