Posts in the “scala” category

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.

Note that 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 2 Example: Making an HTTP GET request with a timeout, using sttp client

As a brief note today, here’s an example of making an HTTP GET request using ZIO 2 and the Scala sttp library. I also let the user specify a “timeout” value, so the request will timeout, rather than hanging.

As a very important note, this is a blocking approach, not a non-blocking approach.

Here’s the source code and Scala 3 + ZIO 2 + sttp function:

Scala: Examples of for-expressions being converted to map and flatMap

Without any explanation, here are a couple of source code examples from my book, Learning Functional Programming in Scala. The only thing I’ll say about this code is that I created it in the process of writing that book, and the examples show how the Scala compiler translates for-expressions into map and flatMap calls behind the scenes.

ZIO 2: A ZIO.timeout interrupt example with ZIO.attempt

As a little ZIO 2 example with Scala 3, here’s some code that starts to show how to use ZIO.timeout along with ZIO.attempt while accessing an internet URL with Scala’s Source.fromURL.

Basically all I’m doing is:

  • I attempt to access a URL using Scala’s Source.fromURL,
  • and then I add a timeout to that, specifically a ZIO##timeout

Here’s the code:

ZIO: How a ZIO value looks like a blueprint in the Scala REPL

A nice thing about using ZIO in the Scala REPL is that it really demonstrates the whole “blueprint” concept. As shown in the example below, after I create the username variable, the REPL shows that username is basically just a data structure. Nothing happens at this time other than the creation of that data structure, which can be executed at a later time.

Scala/Java/Kotlin dates FAQ: How do I calculate the difference between two dates (LocalDate, ChronoUnit)

Scala dates FAQ: How do I calculate the difference between two dates in Scala? That is, while using Scala 2 or Scala 3, you need to determine the difference between two dates. Also, you want to use the newest Java date/time API for this work, such as the date/time API in Java 8, 11, 14, 17, etc.

Solution: Calculating the difference between two dates (in Scala and Java)

If you need to determine the number of days between two dates in Scala — or Java or Kotlin — the DAYS enum constant of the java.time.temporal.ChronoUnit class provides the easiest solution:

ZIO 2: Solution to "ZIO.cond not working" (code not running)

I just wrote the following ZIO 2 question about how to use ZIO.cond to a friend, and got the answer shown. I’ve also added in my own comments where they make sense.

ZIO.done question

Hey, I’m trying to understand why my ZIO failWithMsgEffect doesn’t seem to get run in the following code example?

I have learned that there are better ways to handle this, but I’ve found that if I don’t understand something like this, it will come back to bite me later. Here’s the code:

A ZIO 2 cheatsheet

April, 2024 Update: This ZIO cheatsheet is currently being updated to ZIO 2.x, but it still needs a lot of work.

If you want a good cheat sheet right now, see this one on github. I’m creating my own as I learn ZIO and read the ZIOnomicon book. During the learning process I find that it’s much better to create your own by hand, that way you get something that’s meaningful to you.

Note that almost all of these initial examples come from the ZIOnomicon book and the video that I link to later.

The ZIO Scaladoc

Here’s a link to the ZIO Scaladoc. That’s for the companion object, and this link is for the companion trait.

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.