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

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:

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 I attempt to access a URL using Scala’s Source.fromURL, and then I add a timeout to that, specifically a ZIO##timeout:

As a little note here today, here’s an example of how to use a Scala Either inside a for-expression when using ZIO 2. Specifically I convert each Either into a ZIO in each line of the for-expression.

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