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

As I mention in this blog post, in late November, 2022, my new book, Learn Functional Programming Without Fear, is a best-selling book in Amazon’s Java and functional programming categories.

The book is written for Java, Kotlin, and object-oriented programming (OOP) developers, and as a bonus, I made it less than half the price of other functional programming (FP) books. I hope this makes it a terrific 2022 holiday/Christmas gift for the Java, Kotlin, and OOP programmer in your life.

As of late-November, 2022, my new book, Learn Functional Programming Without Fear, is currently a best-selling programming book in both Amazon’s Java and functional programming categories!

The book is intentionally written for Java, Kotlin, and object-oriented programming (OOP) developers, and I also made the book much less expensive than other FP books to make Scala 3 and functional programming as accessible as possible to everyone. As a result, I hope it makes for a terrific 2022 Christmas/holiday gift for the Java/Kotlin/OOP computer programmer in your life!

Best 2022 holiday programming gift book (for Java/Kotlin/OOP developers)

Demonstrating Scala collections methods with fruit. :) Kudos to whoever came up with this technique first.

Demonstrating Scala collections methods with fruit

I’m glad to report that my book, Learn Functional Programming Without Fear, is the #1 book in both the Java and Functional Programming categories, according to Amazon.com’s book-rating system. (And that doesn’t include many more PDF sales here on Gumroad.com!) Thank you all, and I hope the book is helpful!

#1 best-selling book in Java and Functional Programming

I’m glad to report that my new Scala/FP book — Learn Functional Programming The Fast Way! — is a #1 New Release in the Java Computer Programming category on Amazon.

The book is written for Java, Kotlin, and other object-oriented programming (OOP) developers who want to learn functional programming quickly, so it’s cool to see it achieve this ranking in Amazon’s Java programming category.

2024 Update: The PDF version of the book is now free!

Learn Functional Programming: A #1 new Java release

If you’re interested in the scala.util.Using data type for reading and writing files in Scala, I wrote about it in the Scala Cookbook, and also in my new book, Learn Functional Programming Without Fear. This image on Using comes from that FP book.

scala.util.Using: How to read and write files (Using + Source)

As a quick note, as you can read here on the Scala Contributors website, the scala-cli command in the process of becoming the scala command. I mentioned in my two newest books, Learn Scala 3 The Fast Way! (Book 1: The Adventure Begins) and Learn Functional Programming Without Fear that this might happen. IMHO, Scala CLI is a terrific tool that simplifies the Scala development process — and learning process — and I’m beyond glad to see this.

scala-cli command in the process of becoming scala

IMHO, the song, Anchorage, by Marian Call, is a great song, probably one of my Top-100 songs of all time, maybe Top-50. Of all time.

If you’ve been waiting for a coupon or discount to learn functional programming, today may be your lucky day. I just created a 50% off discount code for the PDF version of “Functional Programming, Simplified” (a best-selling, highly-rated book about functional programming in Scala). But hurry, because the discount is limited to the first 100 buyers.

As I have written before, you can read all those books on the right, or just read the one book on the left, which is based on the best of those books.

If you’d like to get started working with the Scala ZIO functional programming (FP) library, here are two little ZIO 101 and ZIO 102 “Hello, world” examples that you can run with Scala CLI and Scala 3.

FP: ZIO + Scala CLI example: “Hello, world” 101

First, here’s a complete ZIO “Hello, world” example that shows everything you need to get started using ZIO with Scala CLI:

This morning I received the second proof copy of my new book, Learn Functional Programming Without Fear. As soon as I saw it, it reminded me of Steve Martin running around and yelling, “The new phonebook is here, the new phonebook is here!” :)

Learn Functional Programming Without Fear (proof copy)

A little homage to Haskell, Learn You a Haskell for Great Good, Functional Programming, and Halloween.

I like functional programming and what it brings to the table, but this is pretty funny.

I don’t have much time to write today, so very quickly, here are two Zen quotes on non-attachment and duality.

First:

“The Great Way is not difficult for those who don’t make good and bad. When love and hate are both absent, everything becomes clear. Make the smallest distinction, however, and heaven and earth are set infinitely apart.”

Based on my two books, Learn Scala 3 The Fast Way and Functional Programming, Simplified, here’s my video on Expression-Oriented Programming (EOP) in Scala:

As a brief “note to self” today, here are two examples of how to perform SQL queries with Scala 3, using (a) plain JDBC, and also (b) using a little Scala 3 library known as SimpleSQL. These examples use Scala CLI, which as I have mentioned before, makes these examples so much easier to share with you.

Some Zen koans are entirely dependent on you being aware of some long-ago foreign culture, like the one about putting sandals on your head (which apparently was an Asian ritual after a funeral ~2,000 years ago). Other ones, like those from Zen Master Seung Sahn, can be dependent on you knowing his style of teaching.

For this one you need to know almost nothing:

Just as a student sits down for his private face-to-face meeting with a Zen Master in the interview room, the Master yells, “Leave this room!”

So the confused student gets up to leave through the door he came in.

“Not through the door,” the Master yells.

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 personally used this shell script with the exercises in my Learn Scala 3 The Fast Way! book, and I’ll include it with that book’s Github repository shortly.

How to use this scala-cli shell script

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 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 Scala Futures or Akka actors:

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