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

As a brief note, I took a very long time — more than a year — before writing this blog post because I wanted to be sure about what I’ve experienced. But I believe it to be correct, so I’m now sharing it here.

Collagen and diverticula pain

In short, I believe that collagen products like these — which I have bought many times on Amazon, as shown below — help to significantly diverticula/diverticulosis pain:

And when I say significantly, I mean like an 80% reduction in diverticula/diverticulosis pain.

If you come from another programming language like Scala where you can easily create immutable lists — and many other immutable things — working with Dart (and Flutter) can be a major change, possibly because Dart was designed to be a pure OO language.

That being said, I just saw that there is a way to create an immutable list in Dart:

Many times when you use the Dart map method on a list, you’ll write the method as a one-line function. For example, the second line of code here shows a one-line map method on a Dart list:

final xs = ['aaa', 'bb', 'cccc'];
final ys = xs.map((String s) => s.length);  //one-line 'map'
ys.forEach(print);

I’m generally getting out of the writing business, but that being said, I don’t mind sharing some code as I go along. In this case I created the follow Dart programming examples as I reload Dart into my brain:

  • Dart Factory pattern/method example
  • Dart constructors with public and private fields
  • Dart mixin example

As a brief note to self, this weekend I was trying to create a GraalVM native executable named ffx from a Scala project, and I got this error during the GraalVM native-image compilation process:

GraalVM native-image: Warning: Image 'ffx' is a fallback image that requires a JDK
for execution (use --no-fallback to suppress fallback image generation and to print 
more detailed information why a fallback image was necessary).

Then I tried to create the native image using the --no-fallback option, but that failed.

“What is it on this planet that needs doing that I know something about, that probably won’t happen unless I take responsibility for it?”

I found the quote in the book, Wherever You Go, There You Are, by Jon Kabat-Zinn, and the quote itself is from Buckminster Fuller. (The image comes from this page.)

What is it on this planet that needs doing that I know something about?

If you ever want to compile and run a Dart project that uses a pubspec.yaml file to include some dependencies, here’s a quick example of the solution.

Your Dart source code

First, create your code and pubspec.yaml file as usual. Here’s the source code for a file named http_get.dart that uses the http package:

This is a favorite song of late, The Rise, by Lisa Guyer and Sully Erna.

This article shares the source code for a Monte Carlo simulation that I wrote in Scala. It was inspired by the movie Minority Report, as well as my own experience.

Background

For the purposes of this simulation, imagine that you have three people that are each “right” roughly 80% of the time. For instance, if they take a test with 100 questions, each of the three individuals will get 80 of the questions right, although they may not get the same questions right or wrong. Given these three people, my question to several statisticians was, “If two of the people have the same answer to a given question, what are the odds that they are correct? Furthermore, if all three of them give the same answer to a question, what are the odds that they are right?”

As a brief note to self, this is a SQL query I use in the Scala Cookbook to get a distinct list of all movie titles in the MovieLens dataset that are five-star rated in the Comedy/Romance genre:

val q = """
    select distinct(m.title), r.rating, m.genres
    from movies m, ratings r
    where m.movieId == r.movieId 
    and m.genres like '%Comedy%Romance%'
    and r.rating == 5.0
    order by m.title"""

spark.sql(q).show

A quote from Bono about OKRs, from this TED Talk by John Doerr:

“So you’re passionate? How passionate? What actions does your passion lead you to do? If the heart doesn’t find a perfect rhyme with the head, then your passion means nothing. The OKR framework cultivates the madness, the chemistry contained inside it. It gives us an environment for risk, for trust, where failing is not a fireable offense. And when you have that sort of structure and environment and the right people, magic is around the corner.”

“When it’s grim, be the Grim Reaper.”

~ Kansas City Chiefs head coach Andy Reid to Patrick Mahomes (Chiefs v Bills, Jan., 2022)

Two “inspirational quotes” that I saw today:

  • “If you don't cannibalize yourself, someone else will”, Steve Jobs
  • “What can a poor boy do but sing in a rock and roll band?” (from a Rolling Stones song, as discussed by Bruce Springsteen here)

Scala FAQ: How do I get my computer system’s IP address using Scala?

Possible solution: The following code shows one way to get your computer’s IP address using Scala (and Java):

I was having a problem connecting from a Google Cloud Run service to a Cloud SQL instance, so as part of the process of troubleshooting the problem I created the following Scala + MySQL + JDBC database test code. The idea was to create the code as simply as possible — with as few dependencies as possible — to try to understand the problem.

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.7, Creating Meaningful Scala Type Names with Opaque Types.

Problem

In keeping with practices like Domain-Driven Design (DDD), you want to give values that have simple types like String and Int more meaningful type names to make your code safer and easier to read.

Solution

In Scala 3, the solution is to use opaque types to create meaningful type names. For an example of the problem, when a customer orders something on an e-commerce website you may add it to a cart using the customerId and the productId:

A good scene from The Wedding Singer:

Julia: I feel like I’m doomed to wander the planet alone forever.

Robbie: Oh, no. Kind of like the Incredible Hulk, huh?

Julia: Yeah ... only I’m not helping people.

Robbie: Now that’s not true, because I saw inside, you were helping people. You were giving them fish and coffee and forks; people can’t eat without forks.

Julia: And they can’t drink without a fish.

Robbie: That’s right ... I think. What does that mean?

Julia: I don’t know, you lost me back at The Hulk.

This is the beginning of a Docker cheat sheet. I couldn’t find any out here that I liked, so I’m starting to create my own, which will hopefully be organized the way I like it.

This content is cross-posted at My Valley Programming Docker cheat sheet.

Docker Basics

Recommended as a first command on a Docker system:

docker version     # should show Client and Server sections

Docker Lifecycle

This section mostly comes from (https://github.com/wsargent/docker-cheat-sheet):

“The ideal of warriorship is that the warrior should be sad and tender, and because of that, the warrior can be brave as well.”

~ Chogyam Trungpa

As a quick blog post, these are some notes and steps I used to create a Scala JAR file, put that in a Docker image, and then push it up to Google Cloud Run. Rather than add a lot of description here, I’ll just share my notes in a numbered order.

Note that I have cross-posted this article on my Valley Programming website at Pushing a Scala JAR/Docker file to Google Cloud Run to create a service.