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

I recently had a problem with VS Code, Scala Metals, SBT, and Bloop, and VS Code said that I needed to manually run “build import.” Problem is, it didn’t tell me how to run build import.

The short story is that if you need to manually run build import in VS Code, you need to execute the “Import build” command through the VS Code command palette. On a Mac you do this by opening the command palette with the [Cmd][Shift][p] keystroke, and then start typing the word “import”. When you see “Import build” appear in the drop-down list, click it, and it will run the build-import/import-build process.

Back on this day (May 30th) in 2012, I turned myself in to the police in Virginia Beach. What happened was that I accidentally ran a red light the day before — and I mean it was really, really red — the day before. I was trying to figure out how to get to a grocery store and I was looking at everything but the traffic light.

I was about to leave town and didn’t want to deal with this after I left Virginia, so I called the police and told them I ran such and such a light at approximately such and such time, and I’d like to pay my fine before I left town. They had cameras on all four corners so I figured I was screwed. The woman I spoke to worked for a little while, then came back on the phone and said I was fine, they had no record of it.

As a brief note to self, when calling procedures in Scala, the Scala style guide suggests the following:

  1. If the procedure is basically just an accessor, leave the parentheses off
  2. If the procedure has some sort of side-effect, do use the parentheses

Examples of these procedure calls:

val name = person.name   // a procedure that works like an accessor
openTheGarageDoors()     // a procedure that has a side-effect

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 Gumroad.com
(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?

Solution

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
        htmlPane.htmlTextArea.setText(html)
        GuiUtils.showHtmlDialog("Your HTML", htmlPane)
    }
}
Platform.runLater(runnable)

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(
        KeyCode.M,
        KeyCombination.META_DOWN
    )
    scene.addEventHandler(KeyEvent.KEY_RELEASED, (event: KeyEvent) => {
        if (keyComboCommandM.`match`(event)) {
            mainStage.setIconified(true)
        }
    })
}

[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