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

A favorite mindfulness meditation that helps bring me back to the present moment:

In just this moment, what do you feel?
What do you smell?

(Or, more generally, “What do you sense?”)

That isn’t a mantra per se, but more of what I call a “mindfulness reminder” to help bring you back to the present moment.

The Scala SBT build tool uses a file named build.sbt to define your build. The SBT syntax can be a little hard to understand without examples, so I try to collect good examples here when I see them.

Seeing good examples rather than scrolling through reams of documentation has helped me many times!

To that end, this is a build.sbt SBT build file example from the Kyo project:

I recently started having some symptoms of having an infection, especially cold chills and a mild fever, so I began taking big doses of Vitamin C. Yesterday the symptoms went away, so I began to wonder if the Vitamin C helped, and if so, how Vitamin C helps the human body’s immune system. So I asked ChatGPT, and got the following answer, which I have edited for clarity:

Vitamin C, also known as ascorbic acid, plays a crucial role in supporting the human immune system in several ways:

I have no idea if I’ll keep using the script, but for the moment, this is a MacOS script (shell script) I named Turn-Off-Wifi-and-Run-Sleep.command:

time=2

networksetup -setairportpower en0 off
echo "Sleeping $time seconds ..."
sleep $time

osascript -e 'tell application "System Events" to sleep'

I saved this file to my desktop, made it executable with chmod, and then when I run it, it does what its name implies, turning off the Wi-Fi networking and then putting my Mac to sleep.

Note that when you run this script, it opens a MacOS Terminal window to show the result of the commands in the script.

I did this because the Mac had a weird error this morning this morning when I turned it on and it said it had run out of application memory while I was sleeping. ATM I don’t want the Mac working while I’m sleeping, so I wrote this script (though I don’t know how successful this approach will be).

Charlie Munger, who passed away yesterday at the age of 99, once said something to the effect that “Every time you see the word EBITDA, you should think ‘bullshit earnings’” or “Every time you see the word EBITDA, just substitute it with bullshit.”

He had a way with words. :)

As a brief note, here are some tests I just created with Scala 3 to test concepts like private, protected, def, val, and var with inheritance tests, i.e., where classes extend other classes or traits:

// FAILS
// package private_def:
    // trait StoreProduct:
    //     private def id: Int   // compiler:
    //                           // "abstract method id may not have `private` modifier"
    //
    // class Pizza extends StoreProduct:
    //     val id = 1

package def_val:
    trait StoreProduct:
        protected def id: Int

    class Pizza extends StoreProduct:
        val id = 1

package val_val:
    trait StoreProduct:
        protected val id: Int

    class Pizza extends StoreProduct:
        val id = 1

package var_var:
    trait StoreProduct:
        protected var id: Int

    class Pizza extends StoreProduct:
        var id = 1

// FAILS
// package val_var:
//     trait StoreProduct:
//         protected val id: Int
//
//     class Pizza extends StoreProduct:
//         var id = 1      // compiler:
//                         // "variable id of type Int needs to be a stable, immutable value"

// FAILS
// package var_val:
//     trait StoreProduct:
//         protected var id: Int
//
//     class Pizza extends StoreProduct:
//         val id = 1      // compiler:
//                         // "class Pizza needs to be abstract, since protected
//                         // var id_=(x$1: Int): Unit in trait StoreProduct in
//                         // package var_val is not defined"

Hopefully you can see from the comments what works and what doesn’t work when you’re using inheritance in Scala.

As a brief note, the Scala example at this URL (www.geeksforgeeks.org/controlling-method-scope-in-scala/) is not 100% correct, and this code Scala 3 code shows the issue and solution:

package a:
    class ClassA:
        protected var aProtected = 1
        var a = 1

package b:
    import a.ClassA

    class ClassB extends ClassA:
        a = 2
        aProtected = 2                   // ClassB *can* access this field.
        def getAProtected = aProtected   // make that field available to others
                                         //   via this public method.

    object Main:
        def main(args: Array[String]): Unit =
            val b = ClassB()
            b.a
            b.getAProtected   // compiles/works
            // b.aProtected   // does not compile

They correctly note that the variable I call aProtected can’t be accessed from the main method in the second package, but aProtected can be accessed by ClassB, which extends ClassA, though ClassB is in package b and ClassA is in package a.

So if you wanted to see an example of how to access a protected field in Scala code, where your class extends a class in a different package, and that original class has a protected field, I hope this example is helpful.

If you’re (a) on a Mac and (b) have ImageMagick installed and (c) need to convert one or more HEIC files in a directory to JPG files, this command will do the trick:

mogrify -format jpg *heic

I don’t remember if that deletes all the HEIC files, so make sure you back those up before you run this command. I assume you can also use this command to convert the files to PNG, GIF, or other formats, but I’ve only tried converting HEIC files to JPG files so far.

There’s a void in my heart I can’t seem to fill
I do charity work when I believe in the cause
But my soul, it bothers me still
Hey, Lord, you made me like I am
Can You heal this restlessness?
Will there be a void in my heart
When they carry me out to rest?

~ John Mellencamp

A young girl working in a doctor’s office was kind enough to refer to me as an “old guy” a few days ago. Yesterday I watched the first Firefly episode and laughed when I saw Shepherd Book referred to as “grandpa” several times. (“I never married,” he muttered in reply to Kaylee.)

~ March 27, 2014

Oooh, grandpa

“I have no idea who that guy was, but I know that he just reset our karmic destiny.”

Part of a story from the book, Love Everyone.

I have no idea who that guy was, but I know that he just reset our karmic destiny

I honor the place in you
Where the entire universe resides.
I honor the place in you
Of love, of light, of truth, of peace.

I honor the place in you
Where if you are in that place in you and
I am in that place in me,
There is only one of us.

~ The meaning of the word “Namaste,” as interpreted by Ram Dass.

(If I remember correctly, I found this hand-carved Namaste plaque at the Tibetan Cultural Center in Bloomington, Indiana, where I spent some time meditating many moons ago.)

A Namaste plaque

One of my nieces (and/or her friends) seems to be interested in Siberian Huskies, which last night led me to dig out this old photo from a business newspaper in Louisville, Kentucky, in April, 2004.

Me and the dogs in a business magazine

I just ran across this photo of my old-old apartment in Broomfield, Colorado. I liked using a shoji screen to add a temporary “wall” in different spaces, and I used to keep Christmas lights going for most of the winter evenings, as shown.

Shoji screen in my apartment

Rocky Balboa, talking to his son:

“And when things got hard, you started looking for something to blame, like a big shadow. Let me tell you something you already know. The world ain’t all sunshine and rainbows. It’s a very mean and nasty place and I don’t care how tough you are, it will beat you to your knees and keep you there permanently if you let it. You, me, or nobody is gonna hit as hard as life. But it ain’t about how hard you hit. It’s about how hard you can get hit and keep moving forward. How much you can take and keep moving forward. That’s how winning is done.”

I enjoyed this quote from Rocky Balboa the first time I saw the movie, and I appreciate it even more now after getting my a** kicked by this f-ing blood disease, but still grinding along every day.

It's about how hard you can get hit and keep moving forward

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will help to know how it works. Also, when you need to do searches for problems, or when you want to talk to other Scala developers, it will also help to know that when you use the for/yield keywords as shown in these examples, you’re creating something known as a for expression.

How a “for expression” works

Here's a statement of how the yield keyword works in for loops, from the book, Programming in Scala:

MacOS FAQ: How do I “un-hide” a hidden Mac Window (hidden Mac application)?

At one point or another every new macOS user learns that you can easily hide a window on macOS using the [Command][h] keyboard command, but that moment of fun is usually followed by a few moments of panic and terror and the thought “Hey, wait, how do I un-hide/show/display a hidden macOS window?”

Linux ps sort FAQ: Can you share some examples of how to sort the ps command?

Sure. In this article we'll take a look at how to sort the Linux ps command output -- without using the Linux sort command.

Before we get started, the important thing to know is that the Linux ps command supports a --sort argument, and that argument takes a number of key values, and those keys indicate how you want to support the ps output.

Here's a quick look at the --sort information from the ps command man page:

[Note: This is a chapter from a currently-unpublished book I’m (slowly) 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.

My last edits to the Scala Cookbook were in June, 2013, and after all this time there aren’t many things I wish I had added to the Cookbook. Yesterday I ran into one thing that I don’t think I included in the Cookbook: How to process multiple Option values in a Scala for loop (for comprehension). Here’s a quick look at how to do this.

For the impatient

For those who just want to see a for comprehension that processes multiple input Option values, here you go: