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

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.

“I love everybody, that’s what’s killing me.” Cloris Leachman was outstanding in the movie Spanglish, and this is one of my favorite lines. I’ve written about this line and loving everyone from a Buddhist perspective before, and if you’ve ever loved two people and seen them fighting with each other, that’s what this reminds me of.

It also reminds me of war, and what a waste of life it is when people who are in charge ruthlessly kill other people.

I love everybody, that's what's killing me

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

If you’d like to buy a signed copy of the Scala Cookbook, here’s a link to one I have on sale at ebay.

As I note on ebay, I only have seven copies of the Cookbook, and this is Book #2 out of 7.

Buy a Scala Cookbook, signed by Alvin Alexander

NOV., 2022: My new book, Learn Functional Programming The Fast Way, is currently an Amazon Java and functional programming #1 new release. The book is now available in three formats:

PDF Format
$10 (USD) on Gumroad.com

Learn Functional Programming The Fast Way! (PDF Version)

Paperback
$30 on Amazon

Learn Functional Programming The Fast Way (Paperback)

Kindle
$10 on Amazon

Learn Functional Programming The Fast Way! (Kindle Edition)

Functional Programming, Simplified — currently 5-star rated on Gumroad.com, 4.5-star rated on Amazon, and one of the all-time best-selling books on functional programming — is currently on sale in three formats (prices shown in USD):

PDF Format
$15 on Gumroad.com

PDF version of Functional Programming, Simplified

Paperback Book
Now $29.99 on Amazon

Print version of Functional Programming, Simplified

Kindle eBook
$14.99 on Amazon

Kindle version of Functional Programming, Simplified

When I first started meditating in the 1990s, I often had a hard time getting into the proper meditative state when I sat down on the meditation cushion. My “monkey mind” would be jumping all over the place, and it would take me a long time to get it to settle down. Many times I couldn’t even get it to settle down before my 30-minute timer went off.

Because of that, and because I really wanted to become better at meditating, I began experimenting with different ways to get into the meditative state faster.

As a result, this page is a summary of the best ways I know to help you get into a good meditation state when you take time to sit on the meditation cushion (or wherever else you sit). If you’re interested in getting into a deep state fast, these are the “best practices” I know, especially when you’re short on time.

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.

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.

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).

Whenever I think of Father’s Day, I think of the song Papa Was A Rolling Stone: “And when he died, all he left us was a loan...”

(I never knew if the lyrics were supposed to be “alone” or “a loan,” but the latter is more accurate in my case.)

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

Mindfulness/meditation FAQ: What are the reasons, purposes, or motivations to practice mindfulness and meditation?

The motivations to meditate

I just took a little time to share some old notes from my meditation practice about “The purpose of mindfulness.” Or, stated another way, instead of asking about the purposes of being mindful you might ask, “Why bother being mindful?”, or “What are the motivations for practicing mindfulness and meditation?”

In the following sections I describe the purposes and motivations for practicing both mindfulness and meditation.

I don’t know how many people know Ram Dass or have read his writings, but I updated the first motivation here based on his work, because if you really get into mindfulness and meditation, what he states is the end goal.

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

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

Thanks to diverticula, diverticulosis, 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 dates 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: