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

Sister 1: “What about that cute guy I met? Condom Man.”

Sister 2: “Yes, that’s how he likes to be known.”

Sister 3: “Condom Man? Sounds like a superhero.”

~ from the movie, Must Love Dogs

Two sisters talking in the movie, Must Love Dogs:

“So can I ask you a question?”

“No.”

“You never would have left Kevin, would you?”

“If he hadn’t ... left me? No, I don’t think so.”

“But you weren’t really happy.”

“Well, I figured that was the life I picked, so I had to make the most of it. I’m not even sure I deserve a new life now. Sometimes I think that was supposed to be my one chance and I blew it.”

“Where did we get the bad attitudes?”

“The nuns?”

“Yeah, that works. Let’s blame the nuns.”

I need your grace
To remind me
To find my own.

“Remember that you are the Witness only ... even for a moment, do not think that you are the body.”

If you ever want to use ImageMagick to crop a bunch of images that are in one directory, I can confirm that this command crops all the JPG images in the current folder while creating new JPG files with the new name shown:

I saw this 19th century ocean painting on Flipboard. The color/translucence of the water is incredible.

Life-like 19th century ocean painting

If you ever need to send a text message on a macOS system when using Scala (or Java, Kotlin, or other JVM languages), I just tested this function, and it seems to work:

When using Scala (or Java, Kotlin, or other JVM languages), and (a) you want to put a String on the system clipboard and/or (b) get a String off the system clipboard — i.e., copying and pasting text strings — I just used these functions, and can confirm that they work:

As a quick note, here’s a Scala function to generate a random integer value that will be in between the input min and max values:

As a brief note today, here’s a little Scala application that reads an HTML file, parses it with JSoup, and then I select all of the elements with the CSS selector shown. After that, I use some Scala goodness to read all the text values of those elements, see if there is a "W" (win) or "L" (loss) character there, convert that to a Seq[Boolean], and then generated an ASCII Sparkline chart based on those results.

Note that the desired CSS selectors look like this in the HTML:

If you’re interested in Sri Nisargadatta Maharaj’s best books, I share my opinions in this blog post. And just to confirm that I have bought the books I’m talking about, here’s a photo of them:

Sri Nisargadatta Maharaj’s best books for beginners

UPDATE: The latest version of this code is at github.com/alvinj/TextPlottingUtils.

Creating an ASCII version of a Sparkline chart — also known as a Win/Loss chart — in Scala turned out to take just a few lines of code, as shown in this source code:

Creating an ASCII version of a Sparkline chart — or Win/Loss chart — in Scala turned out to take just a few lines of code, as shown in this photo.

Scala: An ASCII Sparkline chart function

As I mentioned previously, the Neotype library is a nice improvement over the verbose Scala 3 opaque type feature. This little example begins to show how much better Neotype is than opaque types, where opaque types require more boilerplate to implement less functionality:

//> using scala "3"
//> using lib "io.github.kitlangton::neotype::0.3.0"

import neotype.*

// [1] Opaque type:
opaque type Username = String
object Username:
    def apply(value: String): Username = value

// [2] Neotype:
object Password extends Newtype[String]:
    override inline def validate(input: String): Boolean =
        input.nonEmpty && input.length > 2

@main def NeotypeTests =

    val u = Username("Alvin")
    println(u)
    
    val p = Password("12")
    println(p)

“...understand always that you are the timeless, spaceless witness.”

As I wrote in my ZIO “mental model” and best practices article, when I work with ZIO, I like to separate (a) my application from (b) the ZIO run value. Specifically I mean that I like to handle the results of the application in the run value. (If you’ve read my previous ZIO blog posts, when I say “application,” I mean our main equation or blueprint.)

There are quite a few different ways to write a ZIO run value, and in this tutorial I want to show many of the different ways I know, or at least those I can remember today. :)

This is an example of a “radar chart,” and comes from this thoughtworks.com page. It looks like a nice way to visualize a set of characteristics. For instance, you could use a chart like this to judge different qualities about a movie, such as whether it is romantic, action-packed, and so on.

Example of a radar chart

“A small team of A+ players can run circles around a giant team of B and C players.” ~ Steve Jobs (image from this twitter page)

That’s true in software, and as I’ve found out recently, it’s true with doctors and other medical professionals.

Steve Jobs on 'A' players

This weekend I read a book by a highly-revered spiritual teacher, and he said that after his enlightenment he initally tried to teach students everything, but as he got older, he felt like he should only teach to a certain point. After that, if a student was curious, he was willing to tell them anything, but he used the students’ curiosity as a way to separate those who were really interested from all others.

Back in the days when I taught Java and OOP courses, I also started by teaching everything, including Design Patterns, but then I realized that that was too overwhelming. So then I also only taught certain topics if a student asked about them (or later, if we offered an advanced course).

So, if you’re ever a student in any subject, I encourage you to ask questions. You never know what a teacher is willing to tell you, if only you’d ask. ;)

Sometimes when you’re working with Scala and want to do things in a functional way, the solution isn’t always clear. For instance, I wanted to write a database query using plain old SQL and JDBC, so to do that, I needed to work with iterating over a ResultSet.

Specifically, I’m writing a little “password manager” application, and for one function I just wanted to return list of all the “app names” stored in the database, where an “app” is something like Gmail, Facebook, Twitter, or any other application or service that requires a username and password.

Scala, functional programming, and working with an iterator