Several weeks after its release, my new book, Learn Functional Programming Without Fear, is still a #1 best-selling functional computer programming book on Amazon.com, as of December, 2022. I hope it will be a great Christmas/Holiday 2022 book gift for the computer geek in your life!
Posts in the “scala” category
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?
- 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
Alternate titles for my book, Learn Functional Programming Without Fear, are, The Fastest Way to Learn Functional Programming and Learn Functional Programming the Fast Way. That’s because I found out — almost by accident — that the fastest way for object-oriented programming (OOP) developers to learn functional programming (FP) goes like this:
I’ve been slowly working on a series of new books, and today I’m proud to announce the first of these:
Starting today you can buy the PDF version of Learn Scala 3 The Fast Way! for just ten dollars — $10 (USD) — at this Gumroad.com URL.
This post is sponsored by my new book,
Learn Functional Programming Without Fear.
ZIO + Scala CLI: “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:
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.
Until some time in the year 2021 I would have written this book using only the Scala SDK and its
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:
Here’s an example of the Scala 3 if-then/else-if/else syntax, as used as the body of a method:
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
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.)
I’m glad to report that my new Scala/FP book — Learn Functional Programming Without Fear — 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 FP quickly, so it’s cool to see it achieve this ranking in Amazon’s Java programming category.
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 on Gumroad.com!) Thank you all, and I hope the book is helpful!
How about a simple functional programming book written with this style:
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.
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!
As a brief note, to help make Scala and functional programming completely free to anyone who wants to learn, I recently created a Kickstarter project for just that: to create free Scala and functional programming training courses. See that Kickstarter page to learn more about this effort.
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)
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.
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