When I began this article I showed that these were the two most complicated type declarations I found in over 150,000 lines of production Java code:
SortedMap<String, Integer> wordCountMap = new TreeMap();
public Class<?> getColumnClass(int columnIndex) {
Conversely, as you saw in the previous example, I just sketched out a little Word Occurrences application in a Scala/FP style, and while writing less than ten lines of code I already used types like these:
Try[Map[Word, Count]]
Either[Throwable, Map[Word, Count]]
So the first obvious thing to say is that in Scala/FP we use types a lot more than developers are used to using in Java/FP.
As someone who’s interested in helping programmers who are interested in making the conversion from Java to Scala, it’s important to acknowledge that this is a different world than Java/OOP. In the Java world you generally only work with types like this if you’re a library developer, but in the Scala/FP world developers at all levels work with them. Therefore, we need to acknowledge this difference in our Scala training materials.
That being said, I’ll ask: Did you find the Word Occurrences example hard to follow? I suspect — and hope — it wasn’t hard to follow. I just sketched out a series of pure functions and their type signatures.
Benefits of Thinking With Types
There are a lot of benefits of “Thinking With Types.” First, here’s some of what you’ve already seen:
- You write pure functions
- Writing pure functions is easy; you don’t have to worry about the state of the entire application, all you have to think about is (a) what comes in and (b) what goes out
- Following the same logic, testing pure functions is also easy
- You can trust pure functions, they don’t lie; their type signatures tell you what’s going on
- When you look at a pure function’s signature and see
Option
,Try
, orEither
, you immediately know, “Something can go wrong here”
Beyond those benefits:
- You’ll start to see solving problems as being like algebra or some other form of math
- As I did with the Word Occurrences example, as you work on solving a problem you’ll sketch out a series of function signatures; the details are important later, but you can solve problems at a high level just by sketching function signatures and working with the types
- As you also saw with that example, the final solution to the problem is just a matter of combining your pure functions
Functional Programming
As you may have guessed, Thinking With Types is a way of describing functional programming (FP), or at the very least, this thought process provides a gateway to FP. If you like the style of programming you saw with the Word Occurrences example, you’ll like FP.
If you’re interested in learning more about functional programming in Scala, here are two more references I’ve written that may help:
Both of those pages are chapters from my book, Functional Programming, Simplified, which is currently the #6 FP book in amazon.com’s Functional Programming category. If you follow that link you’ll find that you can read over 400 pages of that book for free.
I hope this article has been helpful.
All the best,
Al
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |