Posts in the “scala” category

ZIO HTTP: A collection of Route examples

I try not to do this too often, but here’s a currently-incomplete list of ways to define ZIO HTTP routes. I start with some of the simplest examples, and then make them more complicated as I go along:

ZIO: A ZIO 2 + Scala 3 + MySQL database + ZIO HTTP server example application

Without much explanation, the purpose of the following ZIO 2 + Scala 3 code is to show the absolute basics of a working ZIO HTTP + MySQL application. In this case I use the Scalikejdbc library, but as you can see from the code, you can use any Scala, Java, or JVM SQL library you want.

This is almost the “simplest possible”, “Hello world” application that shows all these features.

Very soon I will have a much more complete example including the use of packages, configuration, repository, service, api, and logging, in my free video courses at LearnScala.dev.

Here’s the code:

Last draft of the Scala Cookbook

On May 24, 2013, I finished with the last hardcopy chapters of the Scala Cookbook. I put all of the chapters next to the paper shredder as a way to show what I had just done. The final edits would be finished with a copywriter over the next several weeks, and I signed off on the final edition while I was at Virginia Beach.

Benefits of GraphQL vs REST and gRPC

GraphQL FAQ: What are the benefits of GraphQL, and how does it compare to competing technologies?

This is another blog post in my series of “Conversations with robots.” In this post we look at GraphQL, and the pros and cons of how it compares to REST and gRPC.

2024 holiday season: Reduced prices on my Scala and functional programming Kindle books

I know that a lot of people devalue products when they’re free or inexpensive, and I know this sounds corny, but I always hope that somewhere out there in the world there’s a programmer/developer that works hard, but for one reason or another can’t quite grok Scala and/or functional programming, but then they find one of my books, and the light bulb goes on over their head. And then they start writing better code, and they’re happier with their work, other people are happier with their work, and they make a little more money than they might have made otherwise.

With that in mind, I’ve lowered the price of my Kindle books to just $2.99 (USD) for the 2024 holiday season. Here’s Learn Functional Programming The Fast Way, and here’s Learn Scala 3 The Fast Way.

Also, please note that the PDF versions of these books are FREE.

Enjoy, and I hope they help. :)

The Scala-CLI/Coursier cache location on a Mac/macOS system

As a brief note, my Mac/macOS system disk was pretty full, so I started looking at things I could delete easily. One thing I found is that the Scala-CLI/Coursier cache is location in this Mac directory:

/Users/al/Library/Caches/Coursier

I don’t know yet if it’s safe to delete anything in that directory/folder, but it’s using 7.2GB of space, so I’m about to look into it.

UPDATE: Here is information on deleting the scala-cli/Coursier cache.

Note that I deleted everything in that Coursier directory, and after that NONE of my Java and Scala tools worked, and I had to manually run commands like this to get Java working again:

eval "$(cs java --jvm 11 --env)"

and then later I had problems with scala-cli and had to run this command to kill bloop:

scala-cli --power bloop exit

A ZIO HTTP “Hello, world” example with description and key points

Here’s a brief Scala ZIO HTTP “Hello, world” example from that repository, followed by a description of the code and “key points” to know about the example:

import zio.*
import zio.http.*

object HelloWorld extends ZIOAppDefault:

    // responds with plain text
    val homeRoute =
        Method.GET / Root -> handler(Response.text("Hello, World!"))

    // responds with JSON
    val jsonRoute =
        Method.GET / "json" -> handler(Response.json("""{"greetings": "Hello World!"}"""))

    // create HTTP route
    val app = Routes(homeRoute, jsonRoute)

    // run it like any simple app
    override val run = Server.serve(app)
                             .provide(Server.default)

Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?

Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?

Answer

I’m currently planning a video on “The benefits of an Effect System in computer programming” on my free Scala and functional programming videos website, but in case I don’t get around to making that video, here are the slides I have put together.

Also, if you’d like to see a video that is somewhat similar, here’s my popular “What is ZIO 2?” video on YouTube.

Background

If you haven’t heard of Effects or an Effects System, I wrote about them a long time ago here. Also, in book, Effect-Oriented Programming, the authors describe them this way:

  • Effects are the unpredictable parts of a system.
  • Effect Systems partition the unpredictable parts, and manage them separately from the predictable ones.

Scala: What is the Nothing data type?

Scala FAQ: What is the Nothing type in Scala, and how do I use it?

Solution

In Scala, the Nothing type is called a bottom type, which means it is a sub-type of every other type in the Scala type system. It is also specifically a data type that has no instances.

In practical use, Nothing is used to indicate that a computation or function will never produce a result normally, either because it throws an exception, enters an infinite loop, or encounters some other abnormal termination.

Visually, this is what the Nothing type looks like in the Scala type hierarchy (image courtesy of this scala-lang.org page):

The Scala type hierarchy and the Nothing data type

Scala: Common uses of Nothing

Some common use cases of Nothing in Scala include:

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.