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

I’ve been slowly working on a series of new books, and today I’m proud to announce the first of these:

Learn Scala 3 The Fast Way

Starting today you can buy the PDF version of Learn Scala 3 The Fast Way! for $15 at this Gumroad.com URL.

In the spirit of giving back whatever I can to the Scala community, here’s a very little shell script that I named scw that lets you run the scala-cli command with its --watch option:

#!/bin/sh

# NAME:    scw
# VERSION: 0.1
# PURPOSE: a script that works like a “Unix alias
#          that requires a command-line argument”.

filename=""
if [ $1 ]
then
    filename="$1"
else
    echo "PURPOSE: Run 'scala-cli <filename> --watch'"
    echo "USAGE:   scw <filename>"
    exit 1
fi

scala-cli $filename --watch

I use it with the exercises in my Learn Scala 3 The Fast Way! book, and I’ll include it with that book’s Github repository shortly.

I created this script because I wanted something like a Unix alias to shorten that scala-cli command. When you’re typing that command for more than 80 lessons, every character counts. :) It works like this:

$ scw Functions.sc
4
6
Watching sources, press Ctrl+C to exit.
Compiling project (Scala 3.1.1, JVM)
Compiled project (Scala 3.1.1, JVM)
4
8

Thanks to Scala CLI, that command runs my script, and when I change the script it automatically runs it again.

This article contains a collection of quotes on design from former Apple designer Jonathan Ive (or “Jony Ive,” as Steve Jobs called him). As an interesting note, Mr. Ive prefers to refer to himself as a “builder” or “maker” as opposed to a designer.

For those who don’t know of him, Jonathan Ive is credited with designing almost every Apple product from 1997 until roughly 2020. I (re)discovered Apple with an iPod in 2004, and given that very long string of success, I became interested in what Mr. Ive has to say about design, and to that end, here’s a collection of Jonathan Ive design interview quotes I’ve gathered over the last few years.

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

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 curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my 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.

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.8, Using Term Inference with Given and Using.

Scala 3 Problem

Using Scala 3, you have a value that’s passed into a series of function calls, such as using an ExecutionContext when you’re working with Futures or Akka actors:

“In the beginning, meditation is something that happens within your day. Eventually, the day becomes something that happens within your meditation.”

~ From “The Science of Enlightenment: How Meditation Works

I wanted to create a Unix alias that took an argument (command-line argument), but from what I saw, that wasn’t going to be easy, so I created this little shell script to do what I want. It fails gracefully if you don’t supply a command-line argument, and runs the desired command if you do supply it:

#!/bin/sh

# NAME:    scw
# VERSION: 0.1
# PURPOSE: a script that works like a Unix alias
#          that requires a command-line argument

filename=""
if [ $1 ]
then
    filename="$1"
else
    echo "PURPOSE: Run 'scala-cli <filename> --watch'"
    echo "USAGE:   scw <filename>"
    exit 1
fi

scala-cli $filename --watch

I keep a bin directory in my home directory, and it’s in my PATH, so I just drop this shell script in there and then I can use it like a Unix alias.

You can also use it as an example for any similar script you want to run, i.e., a simple shell script that handles a command-line argument.

My Scala 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

As I continue my quest about Ram Dass and Maya (illusion), I also had a very hard time finding any quotes about Maharaj-ji and Maya, and after sifting through about 1,000 pages of books I finally found this Maharaj-ji quote:

“All is God’s will, but Maya prevents you from knowing it’s all God’s will.”

If you’re interested in this, you can find this quote and a little bit more on page 326 of the book, Miracle of Love.

NOTE: If you read my previous article (a Scala functional programming style To-Do List application), the new content in this article starts down at The Scala/FP Code section.

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.

In functional programming, side effects are kind of a big deal

If you want to get a value out of a Scala Option, there are a few ways to do it. I’ll show those approaches in this article, and then discuss the approach of using the fold method on an Option.

Directly extracting the value out of the Option

A common way to extract the value out of a Scala Option is with a match expression:

I originally wrote a long introduction to this article about how to work with the Scala Option/Some/None classes, but I decided to keep that introduction for a future article. For this article I’ll just say:

  • idiomatic Scala code involves never using null values
  • because you never use nulls, it’s important for you to become an expert at using Option, Some, and None
  • initially you may want to use match expressions to handle Option values
  • as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
  • becoming proficient with higher-order functions (HOFs) like map, filter, fold, and many others are the cure for that verbosity

Given that background, the purpose of this article is to show how to use HOFs rather than match expressions when working with Option values.

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:

I was just looking around for a Ram Dass quote about Maya and couldn’t find anything great, but then I found this YouTube video where he talks about Maya and illusion:

“There’s a philosophy in India where the outside world, the world of things, all is illusion. And that’s a way to deal with the sense world ... to get free of it, and go inward, and go into the Atman, the God within.”

Every once in a while when something hits you, you really remember it; it stands out in your mind as an “Aha” moment. One of those moments for me was when I saw a particular “Model/View/Controller” (MVC) diagram, and the light bulb went on. This diagram made the MVC pattern very simple, and I’ve never forgotten it.

The Model / View / Controller diagram

The diagram I’m talking about comes in two parts. The first MVC diagram shows the symbols the authors use for Model, View, and Controller objects. Here’s that diagram:

Model View Controller - symbols for MVC objects

Maybe because of my Back To Now app, I really like this quote about remembering from Ram Dass:

“I think that remembering is the strategy that most religions are designed to do. It’s remembering there are other planes of consciousness, it’s remembering the illusory nature. It’s remembering Maya, it’s remembering Dukkha. It’s remembering the karma, the sangha, the Buddha, it’s remembering that you’re not caught on one plane of consciousness. It’s reminding you to wake up. The device is to wake you up.”

That quote comes from this ramdass.org page.

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

As I wrote 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).

I drove through Chicago recently and with my crappy old phone’s camera I got this photo of the pink and orange Moon rising over Greater Chicagoland. It was a clear night and the Moon was visible for the entire drive.

Pink/orange Moon rising over Greater Chicagoland

I moved to Colorado after Josh McDaniels was fired by the Broncos, and to say the least, from what I’ve heard on the local radio, he sounds like a completely different coach with the Raiders (per this SI.com article):

“One of the easy things that we’ve tried to keep in mind is we’ll get the best out of everyone here if they love coming to work every day because they love who they’re working for,” said McDaniels, sitting behind his desk. “That sounds so ridiculously simple. Seriously. But it’s the truth. If the players enjoy being coached by us the way we’re coaching them, if the coaches enjoy being treated the way they’re being treated, if the scouts and the personnel department enjoy the way that [GM] Dave [Ziegler] runs the meetings and gives everybody a voice, then when they drive in here in the morning? You should see this.”

“I’d say for me that’s from being a guidance counselor, being a teacher, those experiences,” Ziegler said. “The relationship was really what you had to solidify first to make any progress, whether you’re a classroom teacher or a guidance counselor, and you’re trying to help someone through a problem.”

“They won’t trust you if you don’t,” McDaniels said.

“As a guidance counselor, you have to create it on the front end so if there is a problem down the road, you’ve already created the relationship, so you can help someone that’s having a mental health issue, or going through something else,” Ziegler continued. “The relationship has to come first.”

Summary: This page is a printf formatting cheat sheet or reference page. I originally created this cheat sheet for my own programming purposes, and then thought I would share it here.

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.