How to use ZIO 2 in the Ammonite REPL
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:
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:
NOV., 2022: My new book, Learn Functional Programming The Fast Way, is currently an Amazon Java and functional programming #1 new release. The book is now available in three formats:
PDF Format |
Paperback |
Kindle |
Scala dates FAQ: How do I calculate the difference between two dates? That is, while using Scala — 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.
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:
As a quick note, if you want to embed a Scala source code example in your Scaladoc comments, just put the source code block in between {{{
and }}}
characters in your comments, as shown in this example:
I know that it’s fashionable to say that Scala is dead or something similar to that, but I’ve gotten a few royalty checks from O’Reilly this year for the Scala Cookbook that are amazingly good (💰) for a 3-year old programming book.
Shoot, I’d be happy to receive these royalty payments during the initial months after the book’s release.
As Bjarne Stroustrup said, “There are only two kinds of programming languages: the ones people complain about and the ones nobody uses.”
Functional Programming, Simplified — currently 5-star rated on Gumroad.com, 4.5-star rated on Amazon, and one of the all-time best-selling books on functional programming — is currently on sale in three formats (prices shown in USD):
PDF Format |
Paperback Book |
Kindle eBook |
Scala math FAQ: How do I square a number in Scala, such as squaring an Int
, Double
, Long
, or Float
?
You can square a number in Scala in at least two different ways:
Math.pow
function or the scala.math.pow
functionAmmonite FAQ: How do I load managed dependencies into the Ammonite REPL? What is the syntax for loading dependencies (and JAR files, too)?
If you want to import a managed dependency into the Ammonite REPL, the solution is to use its import $ivy
syntax:
April, 2024 Update: This ZIO cheatsheet is currently being updated to ZIO 2.x, but it still needs a lot of work.
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.
I initially created this “How to use Scala CLI” content for my new Scala book, Learn Functional Programming Without Fear, but when I decided to shorten what I include in the book, I also decided to put the full version of this content here.
Until some time in the year 2021 I would have written this book using only the Scala SDK and its scalac
and scala
commands to compile and run your code, respectively. (These are just like javac
in Java, kotlinc
in Kotlin, and java
with both of those.)
But since that time the Scala CLI command project has come along, and it greatly simplifies the “getting started with Scala” experience, so I use it in this book. Scala CLI:
TIL that you can import/load a JAR file into the Scala Ammonite REPL using this import
command:
import $cp.foo.`simpletest_3.0.0-0.2.0.jar`
Scala date FAQ: How do I format dates (DateTimeFormatter
, LocalDate
) in Scala? That is, when using Scala (2 or 3), how do I format dates, such as for printing them out in a desired format?
The solution is to use the java.time.format.DateTimeFormatter class. It provides three types of formatters for printing date/time values:
Scala date/time FAQ: How do I create new date and time instances with Scala? Specifically, using Scala, how do I create new date and time instances using the Date and Time API that was introduced with Java 8.
Using the Java 8 API and newer — Java 11, 14, 17, etc. — you can create new dates, times, and date/time values. The table below provides a description of some of the new Java date/time classes you’ll use (from the java.time Javadoc), all of which work in the ISO-8601 calendar system.
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.12, How to Parse Scala Strings Into Dates.
While using Scala (Scala 2 or 3), you need to parse a Scala String
into one of the date/time types introduced in Java 8.
If your String
is already in the expected format, pass it to the parse
method of the desired class. If the String
is not in the expected (default) format, create a formatter to define the format you want to accept. The following examples demonstrate the expected formats, and other solutions.
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 software, 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:
As a note to self, I had a problem with the ZIO HTTP library, where it was throwing Netty errors/exceptions like this:
io.netty.channel.AbstractChannel$AnnotatedNoRouteToHostException: null: jsonplaceholder.typicode.com.
The solution to this was to make a couple of changes to my SBT build.sbt file, specifically adding the javaOptions
setting below, and forking the running application from SBT:
As a brief note today, I was starting to look at a free JSON REST web service that to get stock information, and their JSON for a single stock looks like this:
{
"Global Quote": {
"01. symbol": "IBM",
"02. open": "182.4300",
"03. high": "182.8000",
"04. low": "180.5700",
"05. price": "181.5800",
"06. volume": "3037990",
"07. latest trading day": "2024-04-19",
"08. previous close": "181.4700",
"09. change": "0.1100",
"10. change percent": "0.0606%"
}
}
As a little note today, if you want to create a little startup file for the Scala Ammonite REPL, the way you do that on Mac and Unix/Linux systems is to create or edit this file:
~/.Ammonite/predef.sc