The appeal of functional programming (concurrency)
This statement from the book, Learning Concurrent Programming in Scala, sums up the appeal of functional programming in languages like Scala.
This statement from the book, Learning Concurrent Programming in Scala, sums up the appeal of functional programming in languages like Scala.
I’m finally starting to publish my next free video course, called ZIO HTTP + Caliban. You can find it at that website, and it’s free, thanks to the people at Ziverge.
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).
Given that small introduction, here’s my ZIO ZLayer
example, with many notes shown in the comments inside the code:
If you’d like to buy a signed copy of the Scala Cookbook, here’s a link to one I have on sale at ebay.
As I note on ebay, I only have seven copies of the Cookbook, and this is Book #4 out of 7.
As a quick note today, here’s a list of my currently-free books and online video courses about the Scala programming language and functional programming (as of October, 2024):
These books and courses are free thanks to the generosity of Ziverge. If you ever want to pay them back, check out their “on-demand team extension services.”
I just had a situation where I wanted to convert this Scala Map
:
val userPrefs = Map(
"theme" -> "dark",
"lang" -> "en"
)
into a formatted String
that looks like a collection of key/value pairs, like this:
"theme=dark;lang=en"
In short, I found this to be a good solution:
Server-side HTTP FAQ: When you're writing a server-side application, are cookies considered to be part of the header information?
Yes, cookies are part of the HTTP headers. Specifically:
If you’re interested in logging in a ZIO application, the following example shows a collection of different ways you can write log messages. I also show how to create your own custom log format, so the output logging from this application looks like this:
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:
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:
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.
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.
May, 2024: Here’s a summary of my current free online Scala and functional programming training courses:
If you’re interested in 100% Free online Scala and FP video training courses, I hope these online video courses are helpful.
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. :)
Scala 3 FAQ: How does this ZIO HTTP and ZIO JSON code works, specifically the derives JsonEncoder
portion of the code:
case class Greeting(message: String) derives JsonEncoder
In Scala 3, the derives
keyword is used to automatically generate implementations for type classes. Here’s how this works in the context of ZIO JSON.
As a brief note, if you need to debug a ZIO HTTP Request
value, I just created this function to return its values as a String
, and it appears to work properly:
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
Here’s a comprehensive guide to implementing retry logic in ZIO 2 applications, using various scheduling strategies.
Please note that I haven’t double-checked that all of these examples compile as-is, but I do demonstrate many of these in my free Scala and ZIO 2 training videos. (I also added a complete working example at the end.)
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.
Here’s a link to the ZIO Scaladoc. That’s for the companion object, and this link is for the companion trait.
In case these two examples disappear from the ZIO JDBC repository, I have made a copy of them here.
(I say that because earlier this week (late October, 2024) I was told the the ZIO JDBC library is no longer maintained, and then I was also told that they just found a new maintainer.)
That being said, here are the ZIO-JDBC examples. This first one is called Basic.scala
: