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

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.

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:

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

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.

MacOS screen zooming FAQ: Is there a way I can zoom in and zoom out my Mac screen (iMac, MacBook, MacBook Pro, Mac Mini)?

Yes, you can zoom in and zoom out your Mac/MacOS screen. The solutions are shown in the sections that follow. Note that this solution has been tested on macOS — formerly Mac OS X — systems from OS X 10.6 through macOS 10.14.

Mac desktop zoom in/out commands

If you’re using a Mac desktop system (iMac or Mac Pro) with a keyboard and a mouse with a mousewheel (or any sort of mouse-based scrolling system, like a Magic Mouse), just press and hold the [control] key, then scroll the mousewheel up and down, and you’ll see what I mean. The entire Mac screen zooms in and out, just like the “software zoom” on a digital camera.

Here’s another “fake oil painting” I created with Gimp recently. This one is of Thich Nhat Hanh meditating. I don’t remember the original source of this image (before I converted the original photo to an oil painting), but I’m pretty sure I found it on Facebook. On this one I manipulated the colors quite a bit, and also did a Gaussian Blur on the background.

Thich Nhat Hanh fake oil painting with Gimp

If I have my druthers, I’ll finish writing my current book near this wonderful spot, somewhere in Alaska.

A great place to write a book (somewhere in Alaska)

The following are some favorite quotes from Sri Nisargadatta Maharaj and his book, I Am That.

And on a personal note, to think that I have come to understand/know all of these quotes in this lifetime is a little mind-blowing to me.

Here then are the quotes: