Posts in the “scala” category

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.

Yvonne De Carlo in The Ten Commandments (and more)

I recently watched the movie, The Ten Commandments, and when I saw Sephora, I thought, “She looks familiar.”

It turns out she’s played by Yvonne De Carlo. She’s the actress who’s portrayed on my Learn Scala 3 book cover, which is based on the 1950 movie, Buccaneer’s Girl, which she starred in. (Until this, I thought I only knew her as Lily Munster on The Munsters.)

And if you like the movie The Ten Commandments, I also wrote this blog post about The Ten Commandments and the 2024 Presidential Election.

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.

Also, please note that there is a potentially-better solution after this — the one that uses the “view.”

A first solution

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

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

[toc hidden:1]

Scala 3: Functions: Options and Functional Error Handling (Part 1)

NOTE: This is a chapter from my book, Learn Scala 3 The Fast Way. Due to a mistake, this lesson was not included in the book.

When you write functions, things can go wrong. In other languages you might throw exceptions or return null values, but in Scala you don’t do those things. (Technically you can, but other people won’t be happy with your code.)

Instead what we do is work with error-handling data types. To demonstrate this I’ll create a function to convert a String to an Int.

Scala 3 Unions: Simulating Dynamic Typing with Union Types

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.9, Simulating Dynamic Typing with Union Types.

Problem

When using Scala 3, you have a situation where it would be helpful if a value could represent one of several different types, without requiring those types to be part of a class hierarchy. Because the types aren’t part of a class hierarchy, you’re essentially declaring them in a dynamic way, even though Scala is a statically-typed language.

How to read “difficult” Scala method type signatures

When I first saw Scala generic type and multiple-parameter group code like this ~12 years ago, my initial thought was, “Wow, maybe I need to think about a different career”:

def race[A, B](lh: IO[A], rh: IO[B])(implicit cs: ContextShift[IO]): IO[Either[A, B]]

But in the end, as Rocky once said, it ain’t so bad. :)

Rocky: You ain’t so bad

What is “Functional Programming”?

Note: This is an excerpt from my book, Functional Programming, Simplified. (No one has reviewed this text yet. All mistakes are definitely my own.)

Defining “Functional Programming”

It’s surprisingly hard to find a consistent definition of functional programming. As just one example, some people say that functional programming (FP) is about writing pure functions — which is a good start — but then they add something else like, “The programming language must be lazy.” Really? Does a programming language really have to be lazy (non-strict) to be FP? (The correct answer is “no.”)

I share links to many definitions at the end of this lesson, but I think you can define FP with just two statements:

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

I originally wrote a long introduction to this tutorial about how to work with the Scala Option/Some/None classes, but I decided to keep that introduction for a future article. 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

Given that background, the purpose of this article is to show how to use HOFs rather than match expressions when working with Option values.

ZIO ZLayer: A simple “Hello, world” example (dependency injection, services)

As a brief note today, here is some source code for a ZIO ZLayer application using Scala 3. In this code I use the ZLayer framework to handle some dependency injection for a small application. (Note that I don’t like to use the word “simple” when writing about computer programming, but I have tried to make this as simple as I can.)

I’ve commented the code below as multiple “parts” so you can see the thought process of creating an application that uses ZLayer. Basically the idea is that your application needs some sort of service — which might be like a database connection pool, HTTP framework, etc. — and then you make that service available to your application with ZLayer’s provideLayer function (or one of its other functions).

The ZLayer example

Given that small introduction, here’s my ZIO ZLayer example, with many notes shown in the comments inside the code: