How to use ZIO 2 in the Ammonite REPL
Scala/Ammonite FAQ: How do I use ZIO 2 in the Ammonite REPL?
Solution
ZIO can be added into the Ammonite REPL as a managed dependency by using Ammonite’s import $ivy syntax:
Scala/Ammonite FAQ: How do I use ZIO 2 in the Ammonite REPL?
ZIO can be added into the Ammonite REPL as a managed dependency by using Ammonite’s import $ivy syntax:
A cool feature of the Scala REPL is that you can reset/clear a REPL session. To do so, just issue the :reset command, like this:
scala> :reset Resetting interpreter state. Forgetting this session history:
Assuming that you already have at least a little history in your REPL session, the :reset command will show you everything that it dumps, so the full output looks more like this:
When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.
As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:
As a brief note today, this post includes an example that uses Scala 3, scala-cli, and a Typesafe HOCON configuration file. As a bit of an aside, I’m using Scala code like this to create “video” tags for the HTML files in my Scala 3 and ZIO 2 video training courses.
First, here’s the HOCON configuration file, which I named videos.conf:
As I mention in my free functional programming videos, if you had the desire to see your code as math, you might have discovered FP techniques yourself.
As an example, 20 years ago I couldn’t get my sysadmin to install a server-side spam filter at my consulting firm because he was always “too busy.”
So I wrote my own spam-filtering algorithm, and then someone with a CS degree looked at it and said, “That’s a Bayesian algorithm.” I had no idea what that was, I just knew that I was sick and tired of seeing spam all the time.
I later presented this solution at the 2004 Borland Conference (demonstrating how JBuilder helped at that time).
This page contains over 170 examples of how to use the Scala Vector class, covering most of the methods that are available on a Vector, including map, filter, foldLeft, reduceLeft, and many more.
As a brief update, if you prefer videos, here is my Scala Vector class training video.
Use the Vector class when you want to use a general-purpose, immutable indexed sequence in your Scala applications:
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.7, Creating Meaningful Scala Type Names with Opaque Types.
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.
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:
On August 18, 2024, I made the PDF version of Functional Programming, Simplified free, and on August 21st I’m glad to say that we just passed 1,000 free downloads.
August 18, 2024: The PDF version of “Functional Programming, Simplified” is free, at least for now.
As a little note today, here’s an example ZIO 2 application where I do the following:
app) from run using a ZLayerZLayer value in the applicationrun valuerunrun with foldZIOrun value/functionGiven that introduction, here’s the ZIO source code:
ZIO/ZLayer FAQ: How do I use a Java Properties file with ZIO 2 and Scala?
You can use the zio-config library for things like this, but at the moment my preferred approach is to hand-code this ZLayer solution. Maybe that’s because I know how to work with a Java Properties file — i.e., how to read and load it — so I like to see those details.
Therefore, given this Java properties file named application.properties:
As a brief note today, and without much discussion, here’s a ZIO 2 ZLayer example I’ve been working on. The intent of the example is to show one possible way you can enable logging in a ZIO application using ZLayer.
As I mentioned, without much discussion, here’s the ZIO 2 source code:
ZIO/ZLayer FAQ: How do I use a Typesafe Config HOCON properties file with ZIO?
For this ZIO ZLayer solution, you can use the zio-config library for things like this, but at the moment my preferred approach is to hand-code this solution. That’s probably because I’ve written code before to read a HOCON file, so it’s more straightforward atm.
If you’re interested in this sort of thing, I’ve been trying to fix the pages for my free Scala and functional programming video courses, and to that end I needed to do some things with reading HOCON configuration files, and screen-scraping of my own website pages.
Therefore, without much explanation, here is some source code that I wrote over the last day or two, with the help of an A.I. tool or two. One thing to note is that the quality of the code isn’t very good, because I let the A.I. tools generate most of it, and I didn’t bother to clean it up.
As a little Scala note today, here are a few links to some ZIO — ZIO 2 — training videos I have released recently:
I’ll release an entire ZIO 2 video training course soon, so please stay tuned for that.
If you’re ever in Mt. Sterling, Kentucky, one of my nieces owns a used bookstore named Mooncat Book Company, and she now has three signed copies of the Scala Cookbook. :)
My free “Introduction to Functional Programming” video course is almost complete. Today I released 12 new lessons, starting with for Loops, Generators, and Guards, and ending with Lessons Learned.
After this there are about five final lessons (depending on what I decide to include), and by the end of the course we’ll start writing some ZIO code.
As always, many thanks to Ziverge’s Scala and Rust consulting services for sponsoring the creation of these “100% Free” videos, with no ads and no firewalls!
I’m pleased to announce that my 100% Free “Introduction to Scala 3” video training course is now online. I say it’s 100% Free because there are no ads and no paywalls — just free Scala 3 training videos.
This video training course is free thanks to Ziverge, which is sponsoring my work.
March 26, 2024 Update: As a brief note today, here are links to my new, free online video training courses:
As always, these videos are sponsored by Ziverge software consulting (specializing in Rust, Scala, and ZIO).