Posts in the “scala” category

Best FP book for OOP, Java, and Kotlin developers?

October, 2022: I just released my new book, Learn Functional Programming Without Fear, and my aim is to make it a terrific functional programming (FP) book for all OOP developers, especially Java and Kotlin programmers.

As a personal statement, I have no interest in pushing FP on anyone, but if you’re an OOP developer who is interested in FP, I hope this book is helpful. If you have some experience programming in Java, Kotlin, and OOP, I believe you’ll be able to read the whole book over a weekend, or in several nights during a week.

Update: It’s now December, 2022, and the book is still a “#1 New Release” on Amazon!

Why Java and Kotlin?

Why Java and Kotlin?:

  • For both of them, it’s because they’re JVM-based languages (or at least they were initially).
  • For Java developers, it’s because Java has incorporated several aspects of Scala over the last 12 years (including features like lambdas and immutable collections classes)
  • And for Kotlin developers, it’s because Kotlin and Scala are so similar. I’ve seen people say that if you know Scala, you can learn Kotlin in a week, and vice-versa.

FP for Java, Kotlin, and OOP developers

Learn Scala 3 for just $10

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 just ten dollars — $10 (USD) — at this URL.

Scala CLI (Compiling and Running Code)

I initially created this Scala CLI content for my book, Learn Functional Programming Without Fear, but when I decided to shorten what I include in the book, I also decided to put the full version of this content here.

Scala CLI

Until some time in the year 2021 I would have written this book using only the Scala SDK and its scalac and scala commands to compile and run your code, respectively. (These are just like javac in Java, kotlinc in Kotlin, and java with both of those.)

But since that time the Scala CLI command project has come along, and it greatly simplifies the “getting started with Scala” experience, so I use it in this book. Scala CLI:

Scala 2.13’s ‘pipe’ and ‘tap’ chaining operations

The Scala 2.13 collections’ class updates introduced two new “chaining operations” named pipe and tap. Here’s a quick look at how they work, plus a little extra fun at the end.


pipe works like a Unix pipe, passing values to functions. For example, given this setup:

import scala.util.chaining._
import scala.language.implicitConversions

def plus1(i: Int) = i + 1
def double(i: Int) = i * 2
def square(i: Int) = i * i   // we’ll use this one later

For OOP developers: The smallest, simplest functional programming book

Some of the 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 Without Fear. It takes you from Java/OOP to functional programming in the simplest possible step-by-step learning process.

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

Simple FP book: No monads. No monoids. No category theory. Just FP code.

How about a simple functional programming book written with this style:

No monads.
No monoids.
No functors.
No category theory.
Just some Java & Scala code.

Robert Martin’s tweet about writing a new simple functional programming book (in Clojure) was shared with me here, and I like his initial wording on not worrying about monads, monoids, functors, and category theory.

I said almost the exact same thing five years in the beginning of my book, Functional Programming, Simplified, and then in Learn Functional Programming Without Fear I show how to start with Java/OOP code and slowly transition to a functional programming style with Scala/FP examples. Both books focus on pragmatic functional programming source code examples, without getting lost in FP theory.

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

As of late-November, 2022, my new book, Learn Functional Programming Without Fear, is currently a best-selling 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. As a result, I hope it makes for a terrific 2022 Christmas/holiday gift for the Java/Kotlin/OOP computer programmer in your life!

Scala collections: Does filter mean retain/keep, remove

As part of my personal ongoing battle with the meaning of the filter method in functional programming languages like Scala, I decided to create add a keep method to the Scala List class using the Scala 3 extension method approach, like this:

extension [A](xs: List[A])
    def keep(f: (A) => Boolean): List[A] = xs.filter(f)

How to create a Scala 3 infix method (and extension method)

As a brief note, this example shows how to use the Scala 3 infix annotation — @infix. This solution also shows how to use the infix annotation with Scala 3 extension methods, because that’s what I need for my current situation:

import scala.annotation.infix

extension (i: Int)
   @infix def plus(j: Int) = i + j
   @infix def times(j: Int) = i + j

Given those infix + extension method definitions, this is how you use those infix methods:

1 plus 1    // 2
2 times 2   // 4

The REPL confirms this:

scala> 1 plus 1
val res0: Int = 2

scala> 2 times 2
val res1: Int = 4

Before I go, here’s a note from the Dotty / Scala 3 documentation related to infix methods:

A method annotation that suggests that the annotated method should be used as an infix operator. Infix operations with alphanumeric operator names require the operator to be annotated with @infix

In summary, if you wanted to see how to create a Scala 3 infix method, particularly as an extension method, I hope this example is helpful.

How to execute AppleScript from a Java or Scala application

If you ever need to execute AppleScript from a Java or Scala application, this code shows how to solve the programming part of this problem.

Given an AppleScript command as a Scala multiline string, like this:

// press the `fn` key twice
val asCommand = """
tell application "System Events"
    key code 63
    key code 63
end tell

you can execute that command by calling this executeAppleScriptCommand method: