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

Back on June 19, 2012, the Sun looked enormous as it set over the Rocky Mountains, just west of Boulder, Colorado.

Huge Boulder, Colorado sunset, June 19, 2012

My book, Functional Programming, Simplified — 4.5-star rated on Amazon — is currently on sale in three formats:

PDF Format
Just $15 on
(December sale!)

PDF version of Functional Programming, Simplified

Paperback Book
$39.99 on Amazon

Print version of Functional Programming, Simplified

Kindle eBook
$14.99 on Amazon

Kindle version of Functional Programming, Simplified

Scala String FAQ: How can I extract one or more parts of a string that match the regular-expression patterns I specify?


Define the regular-expression patterns you want to extract from your String, placing parentheses around them so you can extract them as “regular-expression groups.” First, define the desired pattern:

val pattern = "([0-9]+) ([A-Za-z]+)".r

Next, extract the regex groups from the target string:

val pattern(count, fruit) = "100 Bananas"

As a brief note to self, this is some code I wrote to update my GUI in a JavaFX application:

val runnable = new Runnable {
    override def run(): Unit = {
        val htmlPane = new HtmlDialogPane
        GuiUtils.showHtmlDialog("Your HTML", htmlPane)

Woke up this morning and my back was a little sore, so before getting out of bed I pulled my legs up over my body in Happy Baby yoga pose — Ananda Balasana, which sounds cool if you say it with an Italian accent. When I did this I looked at my right foot and thought, “That's a handsome foot.”

Of course I wasn’t awake and it wasn’t my foot or my bed or even me, but as feet go, that was a good-looking foot.

~ a facebook post from may 26, 2018

This is a picture of me as a pitcher in high school. I don’t want to say that we were poor growing up, but I wore a large outfielder’s glove while pitching because I was afraid to ask my dad for a new glove, and the shoe on my right foot — the one way up in the air — was cracked in the middle.

(Many thanks to Dwayne Heidtbrink for the photo way back when.)

Alvin Alexander pitching in high school

Last night (May 22, 2014) was one of the “hell nights” I experience once or twice a year. It started with a dream straight out of Carrie or The Exorcist where everything in a room, including me, was being levitated by something, and whatever that thing was, it was not a good thing, it was evil.

After something went flying out the window near the end of that dream, a voice kept saying a two-word phrase over and over again. At 4am this morning I could have told you those words easily, but I can’t remember them now. They were old, Christian/Biblical words, and in short they meant, “Atone for your sins or go to hell.” The two words were something like, “contrition, damnation”, but those aren’t right; I just wanted to be clear that he kept using only two words, over and over again.

For the last several days in dreamland I’ve been working in a large, beautiful log cabin. Along with a group of cohorts — I can’t call them coworkers — we’ve been giving a series of presentations to a group of visitors.

Before my presentation this morning I went out into the audience to talk to a couple of people I had recently met. They were talking about their plans for next weekend, and invited me to join them on Saturday.

Just after this I turned around and saw that one of my cohorts had scribbled a large collection of new notes on my blackboard. Yes, a blackboard with chalk, not a whiteboard with markers. I would later think about this, and wonder if it was influenced by a Columbo episode I watched recently — the one about the architect who likes classical music and kills the millionaire cowboy who likes country-western music. The architect also teaches at a local college, and he and Columbo spent several minutes wiping off chalkboards during one scene.

So I walk up to this cohort who has written notes all over my chalkboards like a mad scientist. I read what he has written, and he asks, “What do you think?”

These days I’m pretty consistent in how I take notes when reading an online article or blog post, so I’m writing a little “CliffsNotes” app to make that process faster. The basic app consists of a browser on the left, and some controls at the bottom of it so I can rapidly put notes in the notepad on the right.

A “CliffsNotes” app

I’ve been stuffing receipts and junk into a compartment in my car for years, and cleaned it out yesterday. This card is by far the best thing I found in there (find Haines, AK on a map, and you might know why). As I’m shredding things, I just saw that on this day two years ago I was in Virginia Beach.

A motel in Haines, Alaska

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that (without a catch clause), so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)

As a brief tip, here’s an example of how to capitalize every word in a string in Scala:

"foo bar baz".split(" ").map(_.capitalize).mkString(" ")

The output of that expression is:

Foo Bar Baz

Also, instead of using the underscore character inside of map, you can write that anonymous function like this, if you prefer:

"foo bar baz".split(" ").map(s => s.capitalize).mkString(" ")

If you wanted to see how to capitalize every word in a Scala string, I hope that’s helpful. Also, if the map method doesn’t make any sense to you, check out my How to write a 'map' function in Scala lesson.

JavaFX TextArea FAQ: How do I insert a string/text into a TextArea at the cursor/caret position?

Solution: First get the position of the cursor/caret in the TextArea, then insert your new text at that position:

int caretPosition = notesTextArea.getCaretPosition;
notesTextArea.insertText(caretPosition, "My new text");

JavaFX tip: To set the minimum width of a dialog, such as a TextInputDialog, use the setMinWidth method on the dialog’s dialog pane, like this:

val d = new TextInputDialog("")
d.setTitle("Open URL")
d.setHeaderText("Open a URL")
d.getDialogPane.setMinWidth(500)  <-- solution

As a brief note, here are a few examples of how I add some keystroke handlers/listeners to a JavaFX project written in Scala:

// add keystroke listeners/handlers to the scene
def addKeystrokeHandlers(scene: Scene, stage: Stage, theMainPane: MainSplitPane): Unit = {
    configureCommandMHandler(scene, stage)
    // other code was here ...

// Minimize (needed to implement manually)
private def configureCommandMHandler(scene: Scene, mainStage: Stage): Unit = {
    val keyComboCommandM: KeyCombination = new KeyCodeCombination(
    scene.addEventHandler(KeyEvent.KEY_RELEASED, (event: KeyEvent) => {
        if (keyComboCommandM.`match`(event)) {

[This is a chapter from a currently-unpublished book I’m writing on meditation and mindfulness.]

As a spiritual being, one possible way to think of life here on Earth is as a “game” that serves as a training ground for the soul. It’s a game like other games, so it has many levels, and they get harder and harder as you progress. So in this case, the better you become at the game of spirituality — the Soul Game — the harder the levels become.

I wasn’t able to take any pictures of them, but last week we had some beautiful full Moon sunsets over the Rocky Mountains. Then I just came across this photo of the Moon and some mountains, with this “true emptiness” quote by Zen Master Seung Sahn. (The image comes from this link.)

True emptiness is before thinking

My phone says I have 18 songs by the Cranberries and Dolores O’Riordan. Some of the favorites are Zombie, Linger, Dreams, Electric Blue, and War Child. RIP, Ms. O’Riordan.

In Jason Quinn’s Inka Speech he describes “original nature” very clearly. When you find the mind before thinking, you find Zen.

For more information, see Zen Master Seung Sahn’s books. I highly recommend Dropping Ashes on the Buddha, and Only Don’t Know. They are both as clear as any Zen books you will ever read.

Original nature, simply stated

In meditation practice you come to a point where you have to “let go.” It’s easy for me to do this at night, I’m usually tired and there isn’t any resistance, but it’s 1,000 times harder during the day. I’ve done it a few times for brief periods and the experience is really remarkable, but I haven’t been able to sustain it yet. No telling what happens when I can sustain it.