Tutorials about the Scala programming language.
I originally wrote a long introduction to this article about how to work with the Scala
Option, but I decided to keep that introduction for a future second article in this series. For this article I’ll just say:
- idiomatic Scala code involves never using null values
- because you never use nulls, it’s important for you to become an expert at using
- initially you may want to use match expressions to handle
- as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
- becoming proficient with higher-order functions (HOFs) like
fold, and many others are the cure for that verbosity
If you ever need to create a large test Scala
Map, I just used this approach and it worked fine:
val map = (for (i <- 1 to 5_000_000) yield (i, 10*i)).toMap
The initial results in the Scala REPL look like this:
val map: Map[Int, Int] = HashMap(4584205 -> 45842050, 2231874 -> 22318740, ...
The code works by creating a series of tuples with the yield expression, where each key is
i, and each value is
Not having a computer science background, I was curious about how to write a Scala function that would find a list of all prime numbers up until some maximum value I supply. I was aware of the Sieve of Eratosthenes, but I didn’t want to implement that, at least not today.
A Scala 3 (Dotty) function
What I ended up doing looks like other Scala prime number solutions you can find on the internet. To make things look a little different, I wrote the code using the current Scala 3 (Dotty) syntax, and the result looks like this:
def isPrime(i: Int): Boolean = if (i <= 1) false else if (i == 2) true else !(2 until i).exists(n => i % n == 0)
It’s super-early in the process, but the Second Edition of the Scala Cookbook is slowly coming to life. I’m currently updating all of the content for Scala 2.13, and when the book is finished it will be updated for Scala 3.
This morning (January 28, 2020) the folks at O’Reilly released the first two chapters of the new, updated book on the O’Reilly Learning Platform. If you have an O’Reilly account you can start reading the new chapters here. If not, you can view the catalog page here.
As a brief note today, here’s an example of the Scala 3 “Dotty” if/then/else-if/else syntax, as used in a function:
def compare(a: Int, b: Int): Int = if a < b -1 else if a == b 0 else 1
Using `then` with if/else
You can also use the
then keyword after your
if expressions, if you prefer:
If you want to see a somewhat larger example of Dotty source code than I’ve shown before, I just took a little time to convert a small Scala 2 project over to the new/current Dotty syntax (i.e., the Dotty syntax supported by the Dotty 0.21 release, circa January, 2020).
I’ve written this before, but when I saw this “pseudocode to Scala code” example in the book Functional Thinking, I thought it was worth mentioning again: If you have trouble grokking the Scala
map method, think of it as being named
transform instead. It transforms an input collection to an output collection, based on the algorithm you supply.
For those coming from the OOP world, I think “transform” is a better word because it is more meaningful, at least initially.
Scala 2.13 introduced two new “chaining operations” named
tap. Here’s a quick look at how they work, plus a little extra fun at the end.