head

Java: A Java list `tail` function (for ArrayList, LinkedList, etc.)

As a brief note today, I was working on a Java/Android application recently, and I needed a “tail” function when I was working on a Java list. What I mean by that is that Scala has a tail function that returns all elements of the list except for the head element, like this:

scala> val x = List(1,2,3,4)
x: List[Int] = List(1, 2, 3, 4)

scala> x.tail
res1: List[Int] = List(2, 3, 4)  //head element removed

and I wanted the same thing in Java.

Shaman

The Native American woman I met last week had an aneurysm and brain surgery last year. (She showed me the scar, and she’s fine now.) Before the aneurysm was discovered, she went to a shaman who’s well-known among Natives here. He lit something, made some smoke, did whatever else he does, then looked at her, put his finger on her forehead and said, “You are blocked here.”

Unfortunately she assumed he was referring to a mental blockage, and thought, “No, I’m an open person, he’s wrong.” Shortly after this, doctors discovered the aneurysm right where he pointed.

This is the story she told me.

How to get the first element from a Scala Set

I have no idea what I was thinking, but today I learned (or was reminded) that if you want the first element from a Scala Set you should use its head method, or headOption. For some reason I kept thinking that take should do the job, but you can see the results in the Scala REPL, where take(1) returns a Set:

Scala Vector filtering methods (examples)

This page contains a collection of examples of filtering methods that can be used with a Scala Vector class. I’ll first show a table that describes the filtering methods, and then show examples of them.

How to show the largest files under a directory on Mac OS X (Unix) alvin December 23, 2017 - 1:00pm

Here’s an example that shows how to find the largest files under a directory on MacOS and Linux/Unix systems.

A du/sort command to show the largest files under a directory on Mac OS X

The Unix/Linux command that worked for me on my MacOS system is this:

$ du -a * | sort -r -n | head -10

du is the disk usage command, and the -a flag says, “Display an entry for each file in a file hierarchy.” Then I use the sort command to sort the du output numerically and in reverse. After that, head -10 shows only the first ten lines of output. In the Music folder on my Mac the command and output look like this:

Woke up with a swollen brain

I woke up this morning and immediately knew something was wrong. I ran into the bathroom, turned on the light, looked in the mirror, and saw that my head looked a lot like a Talosian from Star Trek. “Oh crap,” I thought, “I’m having an allergic reaction to this new medication.” So I went flying out of the apartment and down the hall, yelling, “Help! Someone call 911! Does anyone have some Benadryl?”

Fortunately it was just another dream.

Recursion: How Recursive Scala Function Calls Work alvin May 29, 2017 - 11:01am

An important point to understand about recursive function calls is that just as they “wind up” as they are called repeatedly, they “unwind” rapidly when the function’s end condition is reached.

In the case of the sum function, the end condition is reached when the Nil element in a List is reached. When sum gets to the Nil element, this pattern of the match expression is matched:

Scala: How to extract a column from a list of strings (like awk/print) alvin March 1, 2017 - 9:11am

On Twitter this morning I saw this post by Pablo Fco. Pérez where he compared some Bash commands to Scala. In particular he noted that this awk command:

awk '{print $1}'

is equivalent to:

map(_.split(" ").head)

Burning candle behind your head

That moment where you’ve been working on something really hard at your desk, you solve it, then lean back, swivel your chair to the side, put your feet up on the desk in celebration, and then realize there’s a burning candle behind your head.

=:o