scala

Tutorials about the Scala programming language.

How to use multiple Futures in a Scala for-comprehension

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

Scala: How to use higher-order functions (HOFs) with Option (instead of match expressions)

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 Option, Some, and None
  • initially you may want to use match expressions to handle Option values
  • 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 map, filter, fold, and many others are the cure for that verbosity

How to create a large test Map in Scala (Map class)

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 10*i.

A Scala function to determine whether a number is a prime number

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)

Second Edition of the Scala Cookbook

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.

Scala 3: The Dotty if/then/else-if/else/end syntax

Table of Contents1 - Using `then` with if/else2 - Using `end if` with if/else3 - Closing a function with `end`4 - Discussion5 - Participate/contribute!6 - Attribution

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
Back to top

Using `then` with if/else

You can also use the then keyword after your if expressions, if you prefer:

A small Scala 2 project converted to Dotty

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

Think of the Scala collections’ map method as “transform”

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.