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
Learn Functional Programming Without Fear is written with Scala programming language examples, and a primary reason for that is because Cats Effect and ZIO are two of the leading FP libraries on Earth, and they’re created with Scala. But as you can see in free samples of the book, if you’re a Java or Kotlin programmer, I believe the Scala examples will make sense to you.
I don’t use any special Scala techniques — other than generic types — so if you’re an OOP developer with a little bit of experience with generics, I think you’ll be able to read the code. For example, this is one of the most complicated Scala/FP functions I show in the first 1/3 of the book:
def makeInt(s: String): Option[Int] = try Some(s.toInt) catch case e: NumberFormatException => None
If you’re familiar with the Java
Optional type, I think that Scala function will make sense. You read it like this:
“This is a function named
makeInt. It takes a
Stringinput parameter named
s, and returns an
Intthat’s wrapped inside an
Option. In the function body, it tries to convert the
Int, using the
toIntmethod. If that succeeds, it returns the
Intvalue inside a
Some, but if it fails, it catches the exception and returns a
From a Java/Kotlin perspective, the things you need to know are:
- The preferred way to write Scala 3 code is without most curly braces, and indentation is significant
- (But you can use the “curly braces” style, if you prefer)
Stringtype is backed by a Java
Stringis just like a Java
String, but with a few more capabilities added to it)
Intis like Java’s
int, except that
Intis a class, and it has methods like
Noneare sub-types of
Option[Int]is the function’s return type, which is very similar to Java’s
I speak Java, Kotlin, OOP, and FP
As a final note, I used Java for 15 years, taught C, Java, and OOP classes, and used Kotlin to create Android apps, so I speak Java, Kotlin, and OOP as well as Scala and FP. (I also write OOP-style iOS/Android apps using Flutter and Dart.) In summary, I hope Learn Functional Programming Without Fear will be helpful to you!