Posts in the “scala” category

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

Scala performance FAQ: Is there a reliable way to determine the number of processors (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:

ZIO 2 Example: Print values after a random delay (and the ZIO error channel)

As a brief note today, here’s a little ZIO 2 example that shows how to print a series of values with a random delay in between each value that’s printed.

Also note that in the code below there are different ways to implement randomWaitTimeInSeconds ... for instance, it could return a Duration, but I just have it return an Int.

I also use ZIO.foreach to generate the values in a range, and that could be handled differently.

Another thing I do is use an exception inside ZIO.fail, and I do that because I want that error to be a Throwable on the ZIO “error channel” (i.e., the E parameter in ZIO[R, E, A].)

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.

ZIO 2: How to implement the 'run' value (different solutions)

As I wrote in my ZIO “mental model” and best practices article, when I work with ZIO, I like to separate (a) my application from (b) the ZIO run value. Specifically I mean that I like to handle the results of the application in the run value. (If you’ve read my previous ZIO blog posts, when I say “application,” I mean our main equation or blueprint.)

There are quite a few different ways to write a ZIO run value, and in this tutorial I want to show many of the different ways I know, or at least those I can remember today. :)

Scala tuple examples and syntax

Scala FAQ: Can you share some examples of using tuples in Scala?

A Scala tuple is a class that can contain a miscellaneous collection of elements. I like to think of them as a little bag or container you can use to hold things and pass them around.

You create a tuple with the following syntax, enclosing its elements in parentheses. Here's a tuple that contains an Int and a String:

[toc hidden:1]

Scala “split string” examples (field separator, delimiter)

Scala String FAQ: How do I split a String in Scala based on a field separator, such as a String I get from a comma-separated value (CSV) file or pipe-delimited file.

Solution

Use one of the split methods that are available on Scala/Java String objects. For instance, this example in the Scala REPL shows how to split a string based on a blank space:

Five ways to create a Scala List

Scala List class FAQ: How do I create a List in Scala? (Also asked as, how do I create and initially populate a List in Scala?)

You can create a Scala List in several different ways, including these approaches:

Scala for/yield examples (for-loop and yield syntax)

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will help to know how it works. Also, when you need to do searches for problems, or when you want to talk to other Scala developers, it will also help to know that when you use the for/yield keywords as shown in these examples, you’re creating something known as a for expression.

How a “for expression” works

Here's a statement of how the yield keyword works in for loops, from the book, Programming in Scala:

Scala zip and zipWithIndex examples (with Stream)

I’ve known about using Scala’s zipWithIndex method for quite some time. I used it in for loops to replace counters, and it works like this:

scala> List("a", "b", "c").zipWithIndex
res0: List[(String, Int)] = List((a,0), (b,1), (c,2))

I learned about using zip with Stream last night while reading Joshua Suereth’s book, Scala In Depth. It works like this:

Scala String examples: 100+ examples, methods, anonymous functions (lambdas)

This page contains a collection of over 100 Scala String examples, including string functions, format specifiers, and more. I don’t provide too many details about how things work in these examples, although I do show the output of all the examples. This is mostly just a collection of examples that can be used as a Scala String reference page or cheat sheet.

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.