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

As a brief note today, here is some source code for a ZIO ZLayer application using Scala 3. In this code I use the ZLayer framework to handle some dependency injection for a small application. (Note that I don’t like to use the word “simple” when writing about software, but I have tried to make this as simple as I can.)

I’ve commented the code below as multiple “parts” so you can see the thought process of creating an application that uses ZLayer. Basically the idea is that your application needs some sort of service — which might be like a database connection pool, HTTP framework, etc. — and then you make that service available to your application with ZLayer’s provideLayer function (or one of its other functions).

The ZLayer example

Given that small introduction, here’s my ZIO ZLayer example, with many notes shown in the comments inside the code:

I know that the idea of a “dream vacation” for most people is time at the beach or a beautiful place like Alaska, but once you get to a certain point in meditation, there’s nothing a person would rather do than meditate. So the dream vacation for a meditator is a peaceful, quiet place — both quiet surroundings, and not having to talk to anyone else — where they can meditate, practice yoga, make simple non-meat meals, and go for quiet walks.

Because I have lived in some sketchy places, and other places where people are constantly cutting the grass and running farm machinery, I’ll add that the location should be secure, and again quiet (or at least a place where you know that loud grass-cutting and outdoor activities happen at a certain time). At some point you need to learn to meditate with those issues, but on vacation, no thanks.

I came across this “Al’s Oasis” sign while traveling around the country. I don't remember what state it was in.

Al's Oasis sign

This is a picture of a beautiful painting named Window of the Poet, by Pyotr Konchalovsky. A friend of mine who died a few years ago liked this quite a bit, so it reminds me of her.

Window of the Poet (painting)

In this interview, the interviewer (Steph) asks Shinzen Young about his daily life, and whether he applies any sort of techniques during his normal day. That eventually leads to him saying:

The biggest change is that I don’t have a preference between enlightenment and non-enlightenment. (Note: This is a big change from when he was younger.)

Then, if I’m intending to meditate — in action, in life, like I’m talking to you now ... now I just started to intentionally meditate as we’re talking.

And I typically meditate in the external visual field, and I’m typically meditating on the process of simultaneous expansion and contraction, causing the world in front of me to arise and then disappear. So I’m now applying a formal technique (as we talk).

Interviewer: So I’m arising and disappearing right now.

(Yes) You’re arising and disappearing from the source, moment by moment. Therefore, you appear to me to be the Source. And therefore, very effortlessly, loveable.

Interviewer: I’ll take it.

At the end of this video that’s titled, Inside Shinzen’s Brain: How Shinzen Experiences his Daily Life, he talks about the one obsession he has: “How smart can I be, how creative can I be, in service of the world?”

As a note to self, I had a problem with the ZIO HTTP library, where it was throwing Netty errors/exceptions like this:

io.netty.channel.AbstractChannel$AnnotatedNoRouteToHostException: null: jsonplaceholder.typicode.com.

The solution to this was to make a couple of changes to my SBT build.sbt file, specifically adding the javaOptions setting below, and forking the running application from SBT:

Mac/macOS FAQ: How do I show a file’s character encoding?

Solution

On macOS, you show a file’s character encoding using the -I option of the file command:

As a brief note today, I was starting to look at a free JSON REST web service that to get stock information, and their JSON for a single stock looks like this:

{
    "Global Quote": {
        "01. symbol": "IBM",
        "02. open": "182.4300",
        "03. high": "182.8000",
        "04. low": "180.5700",
        "05. price": "181.5800",
        "06. volume": "3037990",
        "07. latest trading day": "2024-04-19",
        "08. previous close": "181.4700",
        "09. change": "0.1100",
        "10. change percent": "0.0606%"
    }
}

In a Buddha there has never been
Anything that could be said to be there.
Just as a magician
Does not get caught up in his illusions
And therefore by his knowledge
Is not attached to magic forms,
So also the wise in Perfect Enlightenment
Know the three worlds to be like a magic show.
Liberation is merely the end of error.

~ Gampopa

(I saw this quote in the book Be Love Now by Ram Dass.)

When using ScalaTest, Scala, and SBT, and you want to print output in a ScalaTest unit test that you can see, such as printing to STDOUT with println, I just found out that I can do it using its info function, like this:

If you’re interested in meditation and enlightenment/awakening, the book, I Am That, by Nisargadatta Maharaj, has this terrific conversation, which I have shortened slightly:

M: The Guru is concerned little with the person. His attention is on the inner watcher. It is the task of the watcher to understand and thereby eliminate the person.

Q: But the person does not want to be eliminated.

M: The person is merely the result of a misunderstanding. In reality, there is no such thing.

(later)

Q: When will this happen for me?

M: When you remove the obstacles.

Q: Which obstacles?

M: Desire for the false and fear of the true. You, the person, imagine that the Guru is interested in you as a person. Not at all. (He then clarifies what this means.)

As a brief ZIO 2 example, the end of this Scala/ZIO source code shows how to print information to the console in a ZIO application, both in the success case (STDOUT) and also in the failure case (STDERR):

My 100% FREE “Introduction to Functional Programming” online video training course is now finished! To make things a little easier, here are links to all of the free videos in the training course:

March 24, 2024: I just released my free “Advanced Scala 3” online video training course. This free video course gets into different Scala programming topics such as functions, types, generics with variance and bounds, multiversal equality, modular programming, extension methods, and much more.

As always I want to thank Ziverge’s software consulting services for sponsoring these videos! These video courses take many weeks and even months to create, and they would not exist without Ziverge.

<<Click here to start my free Advanced Scala 3 video training course.>>

Advanced Scala 3 video training course

Welcome! This page contains direct links to all of the videos in my 100% Free Scala and FP Video Training Courses. When I say “100% Free”, I mean that there are no ads and no paywalls — all of the videos below are completely free to watch.

My first three courses are listed here, and when I add more free video courses I’ll update this page.

As always I want to thank Ziverge for making this possible! This videos take a long time to create, and I wouldn’t have the time to create these without Ziverge being a sponsor. If you ever want to thank the people at Ziverge, be sure to give them a call when your programming team needs assistance on programming projects. They work with Scala, Rust, A.I., Python, and much more.

April, 2024: As a brief note today, the PDF version of my book, Learn Functional Programming The Fast Way!, is now FREE. I wrote this functional programming book for Scala, Java, and Kotlin developers, and you can now download it for free here:

If you’re interested in functional programming, or just want to learn more about data types, generics, pure functions, expression-oriented programming, and functional error handling, I hope this book is helpful.

Free functional programming book (for Scala, Java, Kotlin, etc.)

As a brief example here today, the following Scala/ZIO source code shows one way to read a file using ZIO 2 and then print its output to the console:

Here are two good quotes from this article, Why companies are leaving the Cloud:

“Another significant driver was the failure to meet internal expectations, at 24%.‘Unmet expectations’ describes most technology trends I’ve been involved with, including client/server, enterprise application integration, service-oriented architecture. and now cloud.

Those surveyed also cited unexpected costs, performance issues, compatibility problems, and service downtime. The most common motivator for repatriation I’ve been seeing is cost. In the survey, more than 43% of IT leaders found that moving applications and data from on-premises to the cloud was more expensive than expected.”