Posts in the “scala” category

Scala performance FAQ: Is there a reliable way to determine the number of CPUs or cores on a computer?

Scala performance FAQ: Is there a reliable way to determine the number of CPUs or cores when writing Scala code?

Solution

As I was reading this article on ZIO performance tuning, I decided to look into whether there is a reliable way to determine the number of CPUs or cores on a computer using Scala (and therefore Java, Kotlin, and other JVM languages).

The solution is that there seems to be a reliable way to determine the number of CPUs or cores in Scala. In short, use the Java Runtime class to get the information. This Scala example shows how to do it:

Scala: How to get the first match in a sequence and immediately return

As a quick note to self, I wrote this Scala code as a way to (a) find the first element in a sequence and then (b) return that element without traversing the rest of the sequence.

I initially thought about writing this code with a while loop, for loop, or for expression — because I knew I needed a loop and a way to break out of a loop — but then I realized that an iterator would help me out here.

A first solution

So without any further ado, here’s this solution:

Scala: How to concatenate two multiline strings into a resulting multiline string

I just had this problem in Scala where I wanted to concatenate two corresponding multiline strings into one final multiline string, such that the elements from the first string were always at the beginning of each line, and the lines from the second string were always second. (When I say corresponding, I mean that the two strings are of equal length.)

That is, given two Scala multiline strings like these:

Scala: How to use ‘fold’ on an Option (syntax, examples)

If you want to get a value out of a Scala Option type, there are a few ways to do it. In this article I’ll start by showing those approaches, and then discuss the approach of using the fold method on an Option (which I’ve seen discussed recently on Twitter).

1) Getting the value out of an Option, with a backup/default value

As a first look at getting values out of an Option, a common way to extract the value out of a Scala Option is with a match expression:

Learn Scala 3 The Fast Way (book)

I’ve been slowly working on a series of new Scala programming books, and today I’m proud to announce the first of these:

Learn Scala 3 The Fast Way! (book cover)

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.

ZIO.attempt: examples and documentation

ZIO 2 FAQ: How do I work with the ZIO.attempt function?

Solution

When you’re working with existing (legacy) Scala code that:

  • is synchronous, and
  • can throw an exception

wrap that code with ZIO.attempt. As you’re about to see, this creates a ZIO effect from that legacy code.

Not only does this create a ZIO effect, it also puts the exception in the ZIO “error channel,” i.e., the E parameter in the ZIO[R, E, A] type signature.

A Scala 3 function that counts the number of vowels in the String it is given as input

As a brief note today, here’s a Scala 3 function that counts the number of vowels in the String it is given as input:

def countVowels(s: String): Int =
    val vowels = Set('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
    s.count(vowels.contains)

Note that this works because as I have mentioned in other places, a Scala Set can be used as a function — specifically as a predicate — and the count function on the Scala sequence classes expects a predicate.

Scala 2 solution

A Scala function to get the Unix epoch time for X days ago

As a brief note today, here’s a Scala function to get the Unix epoch time for X days ago (5 days ago, 10 days ago, etc.):

/**
 * Returns a 10-digit Long (like 1585275929) representing the date/time.
 * Use it to get the time for 1 day ago, 2 days ago, etc. `0` will give
 * you the current time.
 */
def unixEpochTimeForNumberOfDaysAgo(numDaysAgo: Int): Long = {
    import java.time._
    val numDaysAgoDateTime: LocalDateTime = LocalDateTime.now().minusDays(numDaysAgo)
    val zdt: ZonedDateTime = numDaysAgoDateTime.atZone(ZoneId.of("America/Denver"))
    val numDaysAgoDateTimeInMillis = zdt.toInstant.toEpochMilli
    val unixEpochTime = numDaysAgoDateTimeInMillis / 1000L
    unixEpochTime
}

As shown in the comments, if you give it a 0 it will return the current epoch time. As shown by the function’s type signature, the function’s return type is a Long (which is a 64-bit two's complement integer).

Of course you can make the code shorter and better; I just wanted to show the steps in the approach using the Date/Time classes that were introduced in Java 8.