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

“Successful design is not the achievement of perfection but the minimization and accommodation of imperfection.”

~ Henry Petrosky

“My best teachers were not the ones who had all the answers. They were the ones deeply excited by questions they couldn’t answer.”

Chicago Cubs’ star Kris Bryant gets pranked by Hall of Famer Greg Maddux:

Table of Contents1 - Background: What is a Cons cell?2 - What it might look like in Scala3 - Starting to create my own Cons class4 - My second effort5 - Defining my nil value6 - Defining Cons7 - Replacing the NilCons method bodies8 - Adding a toString method to Cons9 - The complete code at this point10 - I’d really like a :: method11 - Interested?12 - See also

For some examples in my new book on functional programming in Scala I needed to create a collection class of some sort. Conceptually an immutable, singly-linked list is relatively easy to grok, so I decided to create my own Scala list from scratch. This tutorial shows how I did that.

Back to top

Background: What is a Cons cell?

The first time I learned about linked lists was in a language named Lisp. In Lisp, a linked list is created as a series of “Cons” cells. A cons cell is simple, it contains only two things:

Here’s a link to some Effective Scala slides, as presented by Mirco Dotta in 2013.

Effective Scala slides

Via a friend on Facebook:

“My advice: Don’t let yesterday contain the greatest things you’ve ever done. Make tomorrow contain your next great goal. Because once you feel your greatest days are behind you, you begin to die.”

At the very least that quote is about the importance of having goals.

“If you think you know what the state of the payments system 10 years out you're in a state of delusion.”

~ Charlie Munger talking about AMEX

I use the Java StringTemplate library in my Android applications, and Brian Clapper has created a Scala wrapper around it that he calls Scalasti. His intro: “Scalasti is a Scala interface to the StringTemplate Java template library. It provides a subset of the features of StringTemplate, using a more Scala-friendly syntax.”

“Matter is frozen energy.” ~ Einstein

Got this in an email last week, seems appropriate for today: “Marry a person you love to talk to.”

“Conscience, when it is flawless, is the voice of our soul, whispering in our ear.”

~ B.K.S. Iyengar, in the book, Light on Life: The Yoga Journey to Wholeness

If it seems like someone is winking at you, a) they might be, it’s Valentine’s week, or, b) they may have a condition known as blepharospasm, which is a symptom of MCAS. My right eye started doing this 10-15 years ago, long before I knew anything about MCAS.

(Turns out there are ~5,000 mast cells per cubic mm of conjunctival tissue, i.e., the inside of the eyelids.)

blepharospasm, or trembling of the eye

Back in the days before global warming the winters could be cold and long, and one year on February 11th I was on a yoga retreat in Mexico, which is where this picture was taken.

In the days before global warming

“The way we experience every part of our lives is affected by the qualities of our mind and by the coloring filters of our mental attitude.”

From the book, When the Chocolate Runs Out

For many years I’ve dreamt of a basement with secret walls and hidden compartments. The basement is underneath a store, and I always assumed that someone lived in the hidden compartments, but I could never find who it was that lived down there, or find all of the compartments.

With my newfound ability to stay conscious most of the night I found all of the compartments this morning, and I was stunned to learn that dozens of people lived in them. I was further surprised that I “knew” most of the people from previous dreams.

At first a few of them tried to kill me. One of them stabbed me with a syringe of some sort. That knocked me down pretty good, and under normal circumstances I might have gone back to sleep, but a few people, including an advanced yoga instructor I met several years ago, helped me recover. When everyone learned that I wouldn't/couldn’t die, they gave up.

I watched the movie Deuce Bigalow a few nights ago, and ever since then I keep hearing the song Call Me by Blondie on the radio. I think the universe may be suggesting a new career path, though I could be reading it wrong.

As a quick note, this stackexchange.com page has some good background information on how to install a deb package file from the command line on Debian Linux (which in my case is Ubuntu 16.04). The short answer is that if you have a deb file named google-chrome-stable_current_amd64.deb, you’ll want to run these two commands, one after the other, as shown:

If you need to dump the contents of an array to the Android Log (Logcat) output, I can confirm that this approach works, at least with simple arrays of integers and strings that know how to print themselves:

Log.i("MyAndroidClass", Arrays.toString(arr));

If you’re trying to print more complicated arrays of custom objects you’ll probably need to implement good toString methods on those objects, and then this technique should work.

As a quick note, if you need some examples of the syntax of how to write a Java method that returns a generic type, I hope these are helpful: