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

When you want to print an sbt project’s version number from the command line — and only the version number — Seth Tisue came up with this answer here on the sbt gitter channel:

When you use sbt — at least with version 1.4.4 in November, 2020 — if you use commands like doc, package, packageDoc, and probably assembly, sbt doesn’t show the directory where your output JAR files are written.

I don’t know why it doesn’t show those output locations — maybe people who use sbt all the time don’t want to see those things, such as if they have sbt doc or sbt package commands in a shell script — but if you want to see the directory and name of the files that sbt creates, use the show command before other commands like doc and package, like this:

[toc]

This page contains a collection of over 100 Scala String examples, including string 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 Scala String reference page or cheat sheet. (I do show the output of most examples.)

If someday the Moon calls you by your name don’t be surprised, because every night I tell her about you.

~ Shahrazad al-Khalij

As I wrote about in this Facebook post, two nights ago I dreamed about a man dying and gently singing a song as he was dying, which led me to listen to The Impossible Dream, from Man of La Mancha, by Brian Stokes Mitchell. Here’s another terrific performance of that same song, as he sings with an orchestra and choir.

As a brief note, if you need to use a varargs parameter with a Scala method, and those varargs parameters should be added to an ArrayBuffer, I can confirm that this code works with Scala 2.13:

import scala.collection.mutable.ArrayBuffer
val xs = ArrayBuffer[String]()

// add one element to the ArrayBuffer
xs += "a"   // ArrayBuffer(a)

// now create a method with a varargs parameter
def appendStrings(strings: ArrayBuffer[String], varargsStrings: String*): Unit = 
    xs.appendAll(varargsStrings)

// now add multiple varargs parameters to the ArrayBuffer
appendStrings(xs, "b", "c")
xs   // ArrayBuffer(a, b, c)

I just confirmed that this code works with Scala 2.13 and Scala 3.0.0

A friend’s young child has been having some serious health problems recently, which led me to this song titled, I Won’t Let Go, by Rascal Flatts:

I will stand by you
I will help you through
When you’ve done all you can do
If you can’t cope
I will dry your eyes
I will fight your fight
I will hold you tight
And I won’t let go

I had forgotten about Rascal Flatts. The last time I thought about them was probably 2010 or earlier. I was sitting at the bar area of a Tumbleweed restaurant in Louisville, Kentucky, and the bartender was trying to tell me how good they were.

This photo from January 12, 2012 of the Rocky Mountains behind Boulder, Colorado made me feel like I was living on the Moon.

Rocky Mountains behind Boulder, Colorado

What the sky will look like when people travel back and forth to the Moon. :)

Traveling to the Moon

[In a Season 1 episode of Fringe, Olivia has been thinking that she’s losing her mind because she’s having dreams where she sees people being murdered, and when she later wakes up and goes to her job as an FBI agent, she finds that they have been murdered. But now they’ve found a potential suspect named Nick Lane, which leads to Olivia and Peter sitting in the lobby of a mental hospital, waiting to meet a doctor there.]

PETER: You know, until this year I’d never actually been to a mental hospital.

OLIVIA: [Bent over, her head in her hands, looking at the floor] Learn to like new things.

PETER: Maybe I never gave it enough thought ... what Walter went through. I only ever saw it through my own perspective. His being crazy was something that he did to us. To my mother and me. It wasn’t something that happened to him.

OLIVIA: Well, you were young.

PETER: Well I’m not young any more. [long pause, looking off into the distance] It must be a terrible thing to not be able to trust your own mind. [sighs]

OLIVIA: Yeah ... [rubs her hands together, then puts her head in her hands again]

Hello Peter, this is me ... your father ... Walter Bishop. (From the tv series, Fringe.)

Hello Peter, this is me ... your father ... Walter Bishop.

“She seduced me with baked goods.”

~ Walter talking about Fauxlivia (alternate universe Olivia) on Fringe

After a brief Gitter discussion that starts here and ends here, I decided to take a little time to see:

  • What it would take to show Scala documentation in the REPL, and
  • Different ways of viewing that documentation

A long time ago, in a place far, far away when I first learned about design patterns in computer programming, I learned that they were based off of this original book by Christopher Alexander, which documents and explains concepts about design in architecture.

One thing that stood out to me in that original book was the following discussion about having views of beautiful scenery. I don’t necessarily agree with it, but I’ve never forgotten it. I found this text on this website, and I’ll leave it here for you as well:

This is the essence of the problem with any view. It is a beautiful thing. One wants to enjoy it and drink it in every day. But the more open it is, the more obvious, the more it shouts, the sooner it will fade. Gradually it will become part of the building, like the wallpaper; and the intensity of its beautify will no longer be accessible to the people who live there.

Therefore:

If there is a beautiful view, don't spoil it by building huge windows that gape incessantly at it. Instead, put the windows which look onto the view at places of transition — along paths, in hallways, in entry ways, on stairs, between rooms.

If the view window is correctly placed, people will see a glimpse of the distant view as they come up to the window or pass it; but the view is never visible from the places where people stay.

As a brief note about catching the [ctrl][c]/ctrl-c signal in a Scala command-line application, I just used this approach on Mac (Unix) system, and was able to catch the ctrl-c signal:

/**
  * The ability to cancel this code with `ctrl-c` inside of 
  * sbt requires you to have this setting in build.sbt:
  * 
  *     fork in run := true
  */
object SlowSocketServer extends App {
    val serverResponseDelayTimeMs = 0
    val serverPort = 5150

    Runtime.getRuntime().addShutdownHook(new Thread {
        override def run = {
            System.out.println("Shutdown hook ran")
            // if (socket != null) socket.close()
            Thread.sleep(1000)
        }
    })
    
    // more code here ...

I was running this application with sbt, and as long as I used that fork setting in the build.sbt file, the ctrl-c keystroke killed my application without also killing sbt, which is what I wanted. This method also printed out the "Shutdown hook ran" string before it died, as desired.

For those who get into meditation and/or enjoy lucid dreams, it’s amazing what comes up during dreams. You meditate hard, maybe practice yoga, work on practices like forgiveness and compassion, try to love everyone, you’re feeling pretty good, and then boom, something comes up in the dream holodeck and you still don’t handle it well.

I guess stuff just keeps coming up out of the subconscious or universe or whatever, and it’s always like, “So, you’re feeling pretty good about your practice? Try this challenge on for size! Too bad, you failed.” It’s like anytime you build up your ego because you think you’re doing well, another test comes up to remind you how far you have to go.

As a quick note, if you need to paste multiple lines of input at one time to the Ammonite REPL, paste your code inside a block. This is particularly helpful when you need to paste something like a companion class and companion object into the Ammonite REPL:

@ {
    // your code starts
    class Foo {
        // code here ...
    }
    object Foo {
        // code here ...
    }
    // your code ends
}

As shown, just include your code inside the opening and closing curly braces.

My book, Functional Programming, Simplified — 4.5-star rated on Amazon, and their 6th-best selling functional programming book — is currently on sale in three formats:

PDF Format
Just $20 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

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

If you know something about Zen, you know that Einstein is writing about the “oneness” of the universe. Zen tries to teach us about this through techniques like Zen Meditation (zazen), and the concept of all things being interdependent.