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

As a brief Scala note today, in Scala 3 it doesn’t look like there’s a way to use something like a package object to make some initial code available to all sub-packages of a high-level package. That is, I’d like to be able to define some types in a top-level package like this:

As a brief Scala ZIO 2 note, if you are using ZIO.attempt and want to show the actual exception you are working with (instead of just Throwable), use refineToOrDie, as shown in this example:

As I work more with ZIO 2, I also find myself thinking a lot about the ZIO 2 mental model, by which I partially mean “a way of thinking about your code” and also “ZIO 2 best practices.”

Here are my initial notes. Also, I hope that most of this code is mine, but several snippets are from other sources that I don’t remember, including the official ZIO 2 docs.

I just wrote the following ZIO 2 question about how to use ZIO.cond to a friend, and get the answer shown. I’ve also added in my own comments where they make sense.

ZIO.done question

Hey, I’m trying to understand why the failWithMsgEffect doesn’t seem to get run in the following code example?

I have learned that there are better ways to handle this, but I’ve found that if I don’t understand something like this, it will come back to bite me later. Here’s the code:

Scala math FAQ: How do I square a number in Scala, such as squaring an Int, Double, Long, or Float?

Solution

You can square a number in Scala in at least two different ways:

  1. Multiply the number by itself
  2. Call the Java Math.pow function or the scala.math.pow function

Scala 3 FAQ: What are opaque types in Scala?

Discussion

I previously wrote a little about Opaque Types in Scala 3, and today, as I’m working on a new video about opaque types, I thought I’d add some more information about them.

Scala dates FAQ: How do I calculate the difference between two dates? That is, while using Scala — 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 date/time FAQ: How do I get the current date (i.e., now or today) in Java? Also how do I get the current time in Java?

Solution

With Java 8 and newer — i.e., Java 11, 14, 17, etc. — use any of the follow “now” methods on these Java classes to get the current data and time:

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 or to display 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:

While many people know the famous Ram Dass book, Be Here Now — which is currently the #1 All-Time Best-Selling book in Spiritualism on Amazon — I’d argue that it’s not necessarily his best book.

For example, I have met a few people in my travels who were struggling to read and understand it it because of all the art and 1960s “hippie style” stuff — which I personally like — and it was at that time I realized that while Be Here Now is his most famous book, it may not be his best book.

Ram Dass’s best books

IMHO, I think the following books are his best, where I believe best will also depend on (a) a person’s experience with his writings and teachings, and also (b) their own progress on the spiritual path.

I don’t know the original source of this image or quote, but I like it.

Impossible ... risky ... give it a try

This is a bit of an unusual note, even for me :), but if you’re ever laying in bed and feel like someone or some thing has gotten in bed with you — but they’re not really there — you may already be asleep and not know it, or you may be having a hypnagogic hallucination.

For instance, last night in bed, while I was waiting to fall asleep, I was practicing a “witness-ing” meditation technique that I have learned. So I’m doing that as intensely as I can, just witnessing my body breathing, and then someone gets in bed with me. So then I realize that I’m probably in the usual “mind awake, body asleep” situation that I have been in a few thousand times in my life.

When I was younger I used to experience sleep paralysis a lot, but these days I can usually just wake myself up, and that’s what I did last night.

I think of this as being asleep and then waking myself up, but other people might say, “No, you must have been having a hypnagogic hallucination.” Personally, I’m about 99% sure that I am asleep, because I was able to do the same thing during an fMRI many years ago, and also during a sleep study test where the tester thought I was asleep because of my brain waves, but I was actually awake.

Ammonite FAQ: How do I use ZIO 2 in the Ammonite REPL?

Solution

ZIO can be added into the Ammonite REPL as a managed dependency by using Ammonite’s import $ivy syntax:

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)

I have often thought that the story of The Odyssey is about temptation and maybe even addiction. And just now I saw this story from the Daily Stoic titled, Discipline Now, Freedom Later, and it offers an interesting take on self-discipline that starts like this:

“At a critical moment in The Odyssey, Odysseus tied himself to the mast of his ship because he knew he wouldn’t be able to resist steering the ship toward the beautiful sound of the Sirens. In temporarily giving up his freedom, Odysseus became the first person ever to hear the Sirens without fatally crashing into the rocks surrounding the island where they lived.”

The story goes on with another quote: “The labor will pass, the rewards will last.”

In a brief personal note, here’s a story about how “Pain is the Doorway.”

Pain is the Doorway

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 the speeches, books, and other writings of Ram Dass, I have put together the following “Ram Dass glossary of terms.”

A few years ago I had a lucid dream that had a beautiful song in it. Unfortunately I didn’t wake myself up right away, and by the time I did wake up, I couldn’t remember all the lyrics. But my favorite lyrics, which set the tone for the song, are:

What do you think about
When you think about me?

So over time I’ve been coming up with other lyrics as I think of them, such as:

Tonight at the table,
Looking out at the sky,
My thoughts were so far away,
I wish I could tell you why.

And also:

After my first-ever bought with diverticulitis, I wanted to make notes about what happened over the last five days.

Monday, April 7, 2015

On April 7, 2015, I woke up at 3:30am with pain and discomfort in my lower-left abdomen. I knew something was wrong, but I didn’t know what it was. It felt like a golf ball was lodged in there, and several hours later it was still there.

Mac OS X launchd FAQ: Can you share some Mac launchd examples (also written as launchd plist examples, or launchctl examples)?

In an earlier tutorial (Mac OS X startup jobs with crontab, launchctl, and launchd) I demonstrated how to use the Mac OS X launchd facility instead of cron to run what would normally be a cron (crontab) job. As I started working with launchd and launchctl, I realized it would probably be helpful to see several different launchd examples, specifically launchd plist file examples.