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

This is my interpretation of the meaning of the Alanis Morissette song, Thank You (also known as Thank U). I don’t claim any special knowledge about the song; this interpretation of the lyrics is just based on my understanding of Zen, Buddhism, and also my practicing of yoga, specifically Iyengar yoga. If you’ve read some of the personal and Zen entries on this website about yoga, mindfulness, meditation, and Zen, you know a lot of what I know.

Here’s one more quote from Michael Piller’s book, Fade In. I think I read that Stephen King and other writers write a similar amount, usually five to ten pages a day. Many beginning writers try to sit down and write an entire book at once, but it’s important to know that writing is more of an endurance contest, not a sprint. Also, when you get tired not only does your productivity drop, but your creativity plummets.

Michael Piller - Fade In - Write six pages a day

As a little ZIO 2 example with Scala 3, here’s some code that starts to show how to use ZIO.timeout along with ZIO.attempt while accessing an internet URL with Scala’s Source.fromURL.

Basically all I’m doing is:

  • I attempt to access a URL using Scala’s Source.fromURL,
  • and then I add a timeout to that, specifically a ZIO##timeout

Here’s the code:

Sometimes when you work as a consultant you get to work on fun, new “greenfield” projects.

Other times, you get called in to clean up a mess.

One day in late-May, roughly 15 years ago, I started a six-figure consulting deal because some applications were a mess. They were failing intermittently, and the company had let one or more developers go.

The 1st Problem

The first problem I ran into was that nobody that was left was sure what code was running in production. Rather than use a repository like Git, the developer had multiple copies of code laying around. From the timestamps you could see what the latest code was, but you couldn’t be sure that’s what was in production.

A nice thing about using ZIO in the Scala REPL is that it really demonstrates the whole “blueprint” concept. As shown in the example below, after I create the username variable, the REPL shows that username is basically just a data structure. Nothing happens at this time other than the creation of that data structure, which can be executed at a later time.

ZIO: How a ZIO value looks like a blueprint in the Scala REPL

IMHO, once you dig into all the eastern religions, you’ll find people saying the same things, and just using different words.

One favorite quote comes from Sri Nisargadatta Maharaj, who said, “Just remember that you are the witness only ... even for a moment, do not think that you are the body.”

Anandamayi Ma was quoted as saying, “My consciousness has never associated itself with this temporary body.”

Those two quotes remind me of this great image that’s based on a Ram Dass quote about humans being spiritual beings who are having a human experience, and not vice-versa. (Also, I apologize, I don’t know who originally created this image, so I can’t give them attribution.)

Ram Dass: spiritual beings who are having a human experience

As a brief note today, here’s a Scala 3 function that counts the number of vowels in the String it is given as input:

def countVowels(s: String): Int =
    val vowels = Set('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
    s.count(vowels.contains)

Note that this works because as I have mentioned in other places, a Scala Set can be used as a function — specifically as a predicate — and the count function on the Scala sequence classes expects a predicate.

Scala 2 solution

As a brief note today, and without much discussion, here’s a ZIO 2 ZLayer example I’ve been working on. The intent of the example is to show one possible way you can enable logging in a ZIO application using ZLayer.

ZIO ZLayer and logging example

As I mentioned, without much discussion, here’s the ZIO 2 source code:

Datline March 22, 2014: A little personal enlightenment:

After going unconscious several times during the last few weeks, I've had conversations with doctors, nurses, friends, and even a shaman about life, death, quality of life, goals, and desires.

I had a hard time answering some of their questions, and yesterday I realized why that was:

If you're truly living in the present moment, those questions don’t make any sense! You can't think about life, death, the past, or the future if you’re fully absorbed in the present moment.

“Are you afraid of dying?” / “I’m sorry, your fear has to do with the future, you’re not talking about the present moment.”

When planning for the future, live fully in that moment of planning for the future. When eating, just eat; and when writing text like this, just write. That’s all.

(In computer parlance, become single-threaded, where that one thread is only focused on HereNow.)

This is an ongoing blog post about “making the switch” from Google AdSense to Ezoic in 2024. If you’re just interested in updates over time, see the “Updates” section at the bottom of this article.

Switching from AdSense to Ezoic

For a long time I was afraid to switch to another company besides AdSense because I had not heard of Ezoic and others, but after several weeks on their platform, I’m happy to say that all is well.

I should also say that in my defense, I was also busy with other things in life, and I thought that “making the switch” might take a while, but if you have existing AdSense and Analytics accounts, it doesn’t seem to take too long. The actual time I spent on a computer to make the changes was probably about 4-8 hours overall, but in calendar time it took a few days because of the AdSense and Analytics integration (waiting), and a few other configuration things I had to wait for. (So, several days in calendar time, but 4-8 hours in actual time.)

It’s also important to note that Ezoic is an AdSense partner, which I discuss more below.

In my post on Ram Dass’s best books — and in other articles on this website — I use words and phrases related to the work of Ram Dass, including terms on yoga, Hinduism, Maharaj-ji, Buddhism, meditation, mindfulness, mantras, and the different names of the man he called Maharaj-ji (aka, Maharaji, Neem Karoli Baba, Neeb Karori Baba). To help understand that article, as well as his speeches, books, and other writings, I have put together the following “Ram Dass Glossary of Terms”, and I hope it’s helpful to others.

This was from a thoughtful gift back in the day. :)

A great Halloween pumpkin squirrel card

I got a “chemo ninja” t-shirt like this one when I was recovering from thyroid cancer. Think I’m gonna need a new one after they remove part of my digestive system in about ten days.

Chemo ninja t-shirt

“I’m going to have to resect the colon.”

~ pretty much every surgeon on M*A*S*H at some point

Thanks to diverticuladiverticulosis, and diverticulitis, in 2018 I had to have a colectomy surgery, which is also known as a colon resection. This page is a diary of my colectomy experience.

Background: Diverticulitis

I had two bouts of diverticulitis in the lower-left portion of my abdomen, once in 2015 and again late in 2017. After the second bout in 2017 the pain never went away completely, and would get significantly worse if I tried to eat normal, high-fiber foods like cereal, wheat bread, broccoli, etc.

Scala date/time FAQ: How do I calculate the difference between two dates in Scala? That is, while using Scala 2 or Scala 3, you need to determine the difference between two dates. Also, you want to use the newest Java date/time API for this work, such as the date/time API in Java 8, 11, 14, 17, etc.

Solution: Calculating the difference between two dates (in Scala and Java)

If you need to determine the number of days between two dates in Scala — or Java or Kotlin — the DAYS enum constant of the java.time.temporal.ChronoUnit class provides the easiest solution:

Java double FAQ: How do I format Java double and float output to two decimal places, such as when I want to format its output for printing — such as currency — or to display it in a user interface? (Also, how do I do the same thing in Kotlin or Scala?)

Solution

There are at least two ways to round a double or float value to two decimal places in Java:

As a brief note today, I have tried quite a few different styles of meditation over the last 10-20 years, and I want to make a note of them here. I’m going to start by listing the meditation teachers I know, write a little bit about each one, and then at the end I’ll try to summarize these teachings.

Scala Either FAQ: How do I get the value out of a Scala Either?

Solution

In this recipe I show many different ways to get the value out of a Scala Either, while accounting for the fact that an Either value may be a success value (a Right) or a failure value (a Left).

I don’t know why they don’t work, but last night for about the 100th or 1,000th time I was reminded that light switches don’t work in dreams.

We were hoping to see the northern lights here last night, so I was intentionally sleeping lightly, waking up every half hour or so. In one dream I looked out my bedroom windows, saw the lights, went out of the room to tell others that we could see the northern lights, tried to turn on a light switch, and it didn’t work. So I tried turning on other light switches, they didn’t work, and then I thought, “Gosh darn it, I’m asleep,” and I woke myself up.

Without much explanation, here’s a Scala 3 and ZIO 2 example that shows how to process command-line arguments (command-line input) in a ZIO 2 application: