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

My 700+ page book, Functional Programming, Simplified — 4.5-star rated on Amazon, their 6th-best selling book on functional programming, and 5-star rated on Gumroad.com — is currently on sale in three formats (prices shown in USD):

PDF Format
$15 (sale!) on Gumroad.com

PDF version of Functional Programming, Simplified

Paperback Book
Now $34.99 on Amazon

Print version of Functional Programming, Simplified

Kindle eBook
$14.99 on Amazon

Kindle version of Functional Programming, Simplified

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

Solution

In Scala 3, 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:

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 24.7, Building Modular Systems with Scala 3.

Problem

You’re familiar with Martin Odersky’s statement that Scala developers should use “functions for the logic, and objects for the modularity,” so you want to know how to build modules in Scala.

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.

These are my notes from creating a Play Framework 2.8 project on January 2, 2022, using Scala 2.13, MySQL, and Slick. I share these notes here because I found it hard to find the correct versions of everything that needed to be used, and the correct syntax in the application.conf file. Unfortunately the Play 2.8 documentation is not up to date, so this was much harder than I expected it to be.

Thanks to Scala being both an object-oriented programming language and a functional programming language, here on the last day of August, 2021, the Scala Cookbook (for Scala 3) is the #1 new release in both the OOP and FP categories.

UPDATE: The book is still #1 in both categories on September 7, 2021 — one month after the Kindle release.

(In the image shown, the Amazon OOP category is on the left, and the FP category is on the right.)

Scala Cookbook: #1 new release in OOP and FP

As this image shows, as of September 14, 2021, the Scala Cookbook is still a #1 new release in both the object-oriented programming (OOP) and functional programming (FP) categories. Because these are the two main computer programming categories, and the Cookbook has been a #1 new release for a month now, it was suggested that it might be a Great 2021 Geek Gift Idea. I can’t argue with that. :)

(The image here shows two browser windows, with the OOP page on the right overlapping the FP page on the left. The images were taken on September 14, 2021.)

Scala Cookbook, a great geek gift idea

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 20.3, Reading a CSV File Into a Spark RDD.

Problem

You want to read a CSV file into an Apache Spark RDD.

Solution

To read a well-formatted CSV file into an RDD:

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 20.2, Reading a File Into an Apache Spark RDD.

Problem

You want to start reading data files into a Spark RDD.

Solution

The canonical example for showing how to read a data file into an RDD is a “word count” application, so not to disappoint, this recipe shows how to read the text of the Gettysburg Address by Abraham Lincoln, and find out how many times each word in the text is used.

After starting the Spark shell, the first step in the process is to read a file named Gettysburg-Address.txt using the textFile method of the SparkContext variable sc that was introduced in the previous recipe:

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 20.1, Getting Started with Apache Spark.

Scala Problem

You’ve never used Spark before, and want to get started with it.

Solution

When you use Spark professionally, your data will probably be spread across multiple computers — maybe thousands of computers — but to get started with Spark you can do all of your work on one computer system in “local mode.” The easiest way to do this is to start the Spark shell, create an array, and go from there.

Back when I was writing Functional Programming, Simplified I started to write a little Scala/FP “To-Do List” application that you can run from the command line, based on a similar application in the Learn You A Haskell For Great Good book. For reasons I don’t remember, I decided not to include it in the book, and forgot about it until I started using GraalVM (“Graal”) recently.

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.8, Formatting Numbers and Currency.

Scala 3 Problem

You want to format numbers or currency to control decimal places and separators (commas and decimals), typically for printed output.

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.9, Creating New Date and Time Instances.

Problem

You need to create new date and time instances using the Date and Time API that was introduced with Java 8.

Solution

Using the Java 8 API you can create new dates, times, and date/time values. Descriptions of common Java 8 Date and Time classes provides a description of some of the new classes you’ll use (from the java.time Javadoc), all of which work in the ISO-8601 calendar system.

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 2.11, Creating Your Own String Interpolator.

Scala 3 Problem

You want to create your own string interpolator, like the s, f, and raw interpolators that come with Scala.

I just ran across this photo of my old-old apartment in Broomfield. I liked using a shoji screen to add a temporary “wall” in different spaces, and I used to keep Christmas lights going for most of the winter evenings, as shown.

Shoji screen in my apartment