Posts in the “scala” category

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

Scala date/time 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:

Scala, JSoup, HTML, CSS selectors, and a Sparkline chart

As a brief note today, here’s a little Scala application that reads an HTML file, parses it with JSoup, and then I select all of the elements with the CSS selector shown. After that, I use some Scala goodness to read all the text values of those elements, see if there is a "W" (win) or "L" (loss) character there, convert that to a Seq[Boolean], and then generated an ASCII Sparkline chart based on those results.

Note that the desired CSS selectors look like this in the HTML:

Neotype as a better solution than Scala 3 opaque types

As I mentioned previously, the Neotype library is a nice improvement over the verbose Scala 3 opaque type feature. This little example begins to show how much better Neotype is than opaque types, where opaque types require more boilerplate to implement less functionality:

//> using scala "3"
//> using lib "io.github.kitlangton::neotype::0.3.0"

import neotype.*

// [1] Opaque type:
opaque type Username = String
object Username:
    def apply(value: String): Username = value

// [2] Neotype:
object Password extends Newtype[String]:
    override inline def validate(input: String): Boolean =
        input.nonEmpty && input.length > 2

@main def NeotypeTests =

    val u = Username("Alvin")
    println(u)
    
    val p = Password("12")
    println(p)

A ZIO 2 cheatsheet

April, 2024 Update: This ZIO cheatsheet is currently being updated to ZIO 2.x, but it’s still a work in progress.

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.

The ZIO 2 “mental model”

As I work more with ZIO 2, I also find myself thinking a lot about the ZIO 2 mental model, by which I partially mean “a way of thinking about your code” and also “ZIO 2 best practices.”

Here are my initial notes. Also, I hope that most of this code is mine, but several snippets are from other sources that I don’t remember, including the official ZIO 2 docs.

Scala 3 opaque types: How to create meaningful type names

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.7, Creating Meaningful Scala Type Names with Opaque Types.

Problem

In keeping with practices like Domain-Driven Design (DDD), you want to give values that have simple types like String and Int more meaningful type names to make your code safer and easier to read.

Solution

In Scala 3, the solution is to use opaque types to create meaningful type names. For an example of the problem, when a customer orders something on an e-commerce website you may add it to a cart using the customerId and the productId:

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: How to use Iterator.continually to loop over a ResultSet iterator (and handling iterating in a functional way to create a list)

Sometimes when you’re working with Scala and want to do things in a functional way, the solution isn’t always clear. For instance, I wanted to write a database query using plain old SQL and JDBC, so to do that, I needed to work with iterating over a ResultSet.

Specifically, I’m writing a little “password manager” application, and for one function I just wanted to return list of all the “app names” stored in the database, where an “app” is something like Gmail, Facebook, Twitter, or any other application or service that requires a username and password.

Scala, functional programming, and working with an iterator

ZIO 2: Passing a ZLayer value to an application, getting a return value, and handling possible errors

As a little note today, here’s an example ZIO 2 application where I do the following:

  • Pass a value to the ZIO application (named app) from run using a ZLayer
  • Use that ZLayer value in the application
  • Pass a value from the application back to the run value
  • Use that value in run
  • Handle any possible errors with foldZIO
  • Show other ways to write the run value/function

Given that introduction, here’s the ZIO source code: