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

Scala date FAQ: How do I format dates (DateTimeFormatter, LocalDate) in Scala? That is, when using Scala (2 or 3), how do I format dates, such as for printing them out in a desired format?

Solution: Scala date formatting

The solution is to use the java.time.format.DateTimeFormatter class. It provides three types of formatters for printing date/time values:

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.12, How to Parse Scala Strings Into Dates.

Problem

While using Scala (Scala 2 or 3), you need to parse a Scala String into one of the date/time types introduced in Java 8, and still used in Java 11, 14, 17, etc.

Scala Solution

If your String is already in the expected format, pass it to the parse method of the desired class. If the String is not in the expected (default) format, create a formatter to define the format you want to accept. The following examples demonstrate the expected formats, and other solutions.

Scala date/time FAQ: How do I create new date and time instances with Scala? Specifically, using Scala, how do I create new date and time instances using the Date and Time API that was introduced with Java 8.

Solution: Creating dates and times in Scala (Java and Kotlin, too)

Using the Java 8 API and newer — Java 11, 14, 17, etc. — you can create new dates, times, and date/time values. The table below provides a description of some of the new Java date/time classes you’ll use (from the java.time Javadoc), all of which work in the ISO-8601 calendar system.

A powerful Scala idiom is to use the Option class when returning a value from a function that can be null. Simply stated, instead of returning one object when a function succeeds and null when it fails, your function should instead return an instance of an Option, where the instance is either:

  1. In the success case, return an instance of the Scala Some class
  2. In the failure case, return an instance of the Scala None class

Because Some and None are both children of Option, your function signature just declares that you're returning an Option that contains some type (such as the Int type shown below). At the very least, this has the tremendous benefit of letting the user of your function know what’s going on.

Some of the computer programming books on the right side of this image are amazing, and I would never discourage anyone from reading the great ones.

But if you’re an object-oriented programming (OOP) developer who wants to start understanding functional programming (FP) over a weekend or a few nights of reading, that’s the goal of the new little book on the left: Learn Functional Programming The Fast Way. It takes you from Java/OOP to functional programming in the simplest possible step-by-step learning process.

FP for Java/Kotlin/OOP developers

I taught Java and OOP for many years, and have used other OOP languages like Kotlin, Python, and Flutter/Dart, so I understand both OOP and FP, and I think that helped to make this a simple book for learning FP.

I also wrote the FP book in the middle — Functional Programming, Simplified — and it’s about three times larger than the little book on the left. I based it on most of the FP books on the right, and it goes into many details that the book on the left doesn’t go into. It’s big, but it’s still easier than reading all the books on the right.

For OOP developers: The smallest, simplest functional programming book

Lately as I’ve been writing and editing Functional Programming, Simplified: Updated for Scala 3, I’ve had the thought, could Functional Programming (FP) be called Algebraic Programming? That is, is “algebraic programming” a more accurate term for this style of programming?

The reason I say this is because, yes, FP is about pure functions, higher-order functions, functions as values, referential transparency, etc., but it’s also about domain modeling (ADTs), immutable variables (algebraic) and immutable data, errors as values, and expression-oriented programming (expressions as equations).

Anyhoo, that’s just a random thought for the day.

As a little update some time later, here are a few additional notes:

  • Functional programmers also use the term “blueprint” a lot, so there might be another possible name in there (something like Blueprint Programming).
  • I’ve read that other people use the name mathematical functions when talking about what I call pure functions.
  • To be clear, I’m not calling for a name change or anything like that, just observing that Algebraic Programming may be a more accurate name.

As a brief note today, I just released the first version of a new, free book/booklet that I’ve titled Learning Recursion.

Learning Recursion: A free booklet by Alvin Alexander

I don't consider myself an Apple fanboy, but I am a Jonathan Ive hardware design fan. I have a great respect for the industrial design work he and his team do. It’s safe to say that if I were in college these days I’d be studying industrial design instead of the aerospace engineering degree I got way back when.

Apple mission statement: To the best of my knowledge Apple has never published a “mission statement,” but I enjoyed this quote from Tim Cook of Apple regarding Apple's business philosophy, which is essentially their mission statement:

Linux crontab FAQ: How do I edit my Unix/Linux crontab file?

I was working with an experienced Linux sysadmin a few days ago, and when we needed to make a change to the root user crontab file, I was really surprised to watch him cd to the root user’s cron folder, make changes to the file, then do a kill -HUP on the crontab process.

Thinking he knew something I didn’t know, I asked him why he did all of that work instead of just entering this:

Linux grep FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name grep means “general regular expression parser,” but you can think of the grep command as a search or find command for Unix and Linux systems: It’s used to search for text strings and regular expressions within one or more files.

I think it’s easiest to learn how to use the grep command by showing examples, so let’s dive right in.

.

.

.

.

.

(this section left blank for the long Table Of Contents over on the right)

.

.

.

.

.

Linux/Unix FAQ: Can you share some Linux find command examples?

Sure. The Linux find command is very powerful. It can search the entire filesystem to find files and directories according to the search criteria you specify. Besides using the find command to locate files, you can also use it to execute other Linux commands (grep, mv, rm, etc.) on the files and directories that are found, which makes find even more powerful.

Java enumerations FAQ: Can you share some Java enum examples, such as how to declare a Java enum, and how to use a Java enum in a for loop, if/then statement, and Java switch statement?

Sure. As described in the Sun/Oracle Java documentation, “you should use enum types any time you need to represent a fixed set of constants.” Let's take a look at some enum examples to see how this works.

Summary: This tutorial shows how to use Unix/Linux curl command to create shell scripts to test REST/RESTful web services.

Background: How to test REST services and microservices

There may be better ways to do this, but when I was writing a mobile app — with a JavaScript client written in Sencha Touch, and the server written with the Play Framework — I wrote some Unix curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my server-side Play Framework REST/RESTful web services.

You can also write REST clients with Scala, Java, and other languages, but for various reasons I wanted to test these web services with curl. As an added bonus, you can include scripts like this into your testing and integration process, if you like. You can also use the same approach with Nagios to make sure your service is still running.

I recently bought a new iMac, and it came with a “Magic Mouse 2,” which is like the original Magic Mouse, but this one has a built-in battery. (It would have been nice if the documentation mentioned that, but that’s another story.) Once I figured out that you could charge it, I quickly wondered, “How long do you need to charge the Magic Mouse 2?”

“Why am I always sick?” That’s a question I used to ask myself a lot, and other people asked it as well: “Al, why are you always sick?”

“You sure do get sick a lot”

I remember one time I was in the same room as my wife while she was on the phone. She was taking to her sister, who was talking about her husband (my brother-in-law), and their conversation went on for quite some time. Afterwards I said, “Wow, I hope you guys don’t talk about me all the time like that.” My wife said, “No, we just say that you seem to get sick a lot.”

Kotlin FAQ: How do I iterate over the elements in a Map in Kotlin?

Solution

Here’s an example that shows how to iterate over the elements in a Kotlin Map using a for loop:

val map = mapOf("a" to 1, "b" to 2, "c" to 3)

for ((k,v) in map) {
    println("value of $k is $v")
}

When you get started with functional programming (FP) a common question you’ll have is, “What is an effect in functional programming?” You’ll hear advanced functional programmers use the words effects and effectful, but it can be hard to find a definition of what these terms mean.

Effects are related to monads (but don’t worry)

A first step in the process of understanding effects is to say that they’re related to monads, so you have to know just a wee bit about monads to understand effects.

But fear not, this is surprisingly simple. As I write in my book, Functional Programming, Simplified, a slight simplification is to say that in Scala, a monad is any class that implements the map and flatMap methods. Because of the way Scala for-expressions work, implementing those two methods lets instances of that class be chained together in for-expressions (i.e., for/yield expressions).

In a 2023 trip to New Mexico, I found out that The Sage Hotel in Santa Fe has bed bugs, and since then I’ve been trying to learn more about them. I asked ChatGPT, “What are bed bugs,” and got this answer (which I edited slightly for brevity and clarity):

Bed bugs are small, parasitic insects that feed on the blood of humans and animals. The common bed bug, Cimex lectularius, and the tropical bed bug, Cimex hemipterus, are the two main species that feed on humans. Bed bugs belong to the family Cimicidae and the order Hemiptera.

Key characteristics of bed bugs include:

  • Size and appearance
  • Feeding behavior
  • Habitat
  • Reproduction
  • Resilience

Signs of a bed bug infestation include itchy welts or bites on the skin, small reddish-brown bugs on bedding or furniture, and dark spots (fecal matter) on surfaces.

See my Santa Fe bed bug report for images of actual bed bugs, courtesy of the Sage Inn in 2024.

After learning that the Sage Hotel in Santa Fe, New Mexico has bed bugs in 2023, I’ve been trying to learn more about them. I asked ChatGPT how long bed bugs have been around, and it provided this answer (which I edited a little bit for brevity and clarity):

Bed bugs have been around for a very long time, with evidence suggesting they have been associated with humans for thousands of years ... Historical records indicate that bed bugs were present in ancient Egypt, Greece, and Rome. They have been mentioned in ancient writings, including early medical texts. Bed bugs became more widespread in Europe during the 17th century and reached their peak infestation levels in the 18th and 19th centuries.