Posts in the “scala” category
Scala dates FAQ: How do I calculate the difference between two dates (LocalDate, ChronoUnit)
Scala dates FAQ: How do I calculate the difference between two dates? That is, while using Scala — Scala 2 or 3 — you need to determine the difference between two dates.
Solution: Calculating the difference between two dates (in Scala and Java)
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:
SBT/build.sbt: One percent symbol or multiple percent symbol for Java and Scala dependencies?
As a brief note today, when you’re using SBT and its build.sbt file with Scala projects, use %%
in library dependencies when referring to Scala-specific libraries, and use only one %
character when referring to other JVM dependencies, like Java dependencies. Here are some examples of this syntax:
Scala SBT: How to “re-run with -deprecation” (or -feature)
Scala FAQ: When compiling a Scala application with SBT, I get warning messages like these:
$ sbt compile [warn] there were 6 deprecation warnings; re-run with -deprecation for details [warn] there were 4 feature warnings; re-run with -feature for details
How do I ’re-run with -deprecation’ or ’re-run with -feature’?
Scala: How to use startsWith tests in match/case expression
Scala FAQ: How can I use the startsWith
method on a Scala String
to match multiple possible patterns in a match
expression?
Solution
As shown in the following example, you can use the startsWith
method on a String
to match multiple possible patterns in a match
expression. startsWith
checks to see if a String
starts with the prefix (or substring) you specify, so although in these examples I use complete strings, you can also use regular expression patterns.
Example: startsWith + match expression
Free functional programming book (free PDF for Scala, Java, Kotlin, etc.)
As a brief note today, the PDF version of my book, Learn Functional Programming The Fast Way!, is now FREE. I wrote this functional programming book for Scala, Java, and Kotlin developers, and you can now download it for free here:
If you’re interested in functional programming, or just want to learn more about data types, generics, pure functions, expression-oriented programming, and functional error handling, I hope this book is helpful.
Scala Date FAQ: How do I format dates (DateTimeFormatter, LocalDate) in Scala?
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?
Solution: Scala date formatting
The solution is to use the java.time.format.DateTimeFormatter class. It provides three types of formatters for printing date/time values:
Scala FAQ: How Do I Create New Date and Time Instances with Scala
Scala 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.
Solution: Creating dates and times in Scala (Java and Kotlin, too)
Using the Java 8 API and newer, 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.
Scala 3 Opaque Types: When to use, examples, benefits
Scala 3 FAQ: What are opaque types in Scala?
Discussion
I previously wrote a little about Opaque Types in Scala 3, and today, as I’m working on a new video about opaque types, I thought I’d add some more information about them.
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):
Scala: Common uses of Nothing
Some common use cases of Nothing
in Scala include:
Scala: How to square a number (Int, Double, Float, Long)
Scala math FAQ: How do I square a number in Scala, such as squaring an Int
, Double
, Long
, or Float
?
Solution
You can square a number in Scala in at least 2-3 different ways:
- Multiply the number by itself
- Call the Java
Math.pow
function or thescala.math.pow
function
A Scala current date and time example
Scala date/time FAQ: How do I get the current date and time in Scala?
The following code demonstrates how to get the current time in Scala, and then further shows how to get other information, such as the current minute, using the Java SimpleDateFormat class:
My free Scala and FP online video training courses
Welcome! This page contains direct links to all of the videos in my 100% Free Video Training Courses. When I say “100% Free”, I mean that there are no ads and no paywalls — all of the videos below are completely free to watch.
My first three courses are listed here, and when I add more free video courses I’ll update this page.
As always I want to thank Ziverge for making this possible! This videos take a long time to create, and I wouldn’t have the time to create these without Ziverge being a sponsor. If you ever want to thank the people at Ziverge, be sure to give them a call when your programming team needs assistance on programming projects. They work with Scala, Rust, A.I., Python, and much more.
Functional Programming, Simplified (a best-selling FP book)
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/Java/Kotlin: How to replace left and right brackets in a String (replaceFirst, replaceAll)
Scala/Java/Kotlin String
FAQ: How do I replace left and right brackets — the [
and ]
characters — in a String
when using methods like replaceFirst
and replaceAll
?
Solution
If you’re using Scala, Java, Kotlin, or other JVM languages, and need to replace left or right brackets in a String
, I found the following solution, which seems to work well with String
methods like replaceFirst
and replaceAll
.
A Scala Either, Left, and Right example (like Option, Some, and None)
Summary: This post is a discussion of the “Option
/Some
/None
Pattern” in Scala, specifically how to use the Either
/Left
/Right
data types instead of Option when you need to know the reason some code failed. As you may know, the None data type does not return failure/exception information, but if you use the Either
/Left
/Right
types, you can access that failure information through the Left
type.
The post is sponsored by my book, the 2nd Edition of the Scala Cookbook.
Scala for/yield examples (for-loop and yield syntax)
I just found some notes from when I first began working with Scala, and I was working with the yield
keyword in for
loops. If you haven't worked with something like yield
before, it will help to know how it works. Also, when you need to do searches for problems, or when you want to talk to other Scala developers, it will also help to know that when you use the for
/yield
keywords as shown in these examples, you’re creating something known as a for expression.
How a “for expression” works
Here's a statement of how the yield
keyword works in for
loops, from the book, Programming in Scala:
Scala List class examples: range, fill, tabulate, appending, foreach, more ...
Scala List FAQ: Can you share some Scala List
class examples?
The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.
In this tutorial, I'll share examples of the most common List
operations (methods).
How to add elements to a List in Scala (List, ListBuffer)
Scala List FAQ: How do I add elements to a Scala List
?
Solution
"How do I add elements to a Scala List
” is actually a trick question, because you can't add elements to a Scala List; it's an immutable data structure. If you’ve ever used the Java String
type, it’s just like that.
That being said, in the following sections I’ll show what you can do.
Prepending elements to Scala Lists
The most common way to “add” elements to a Scala List
is to create a new List
from an existing List
by prepending elements to the existing list. We do this all the time in functional programming in Scala, and the general approach looks like this in the Scala REPL:
Free Scala 3 online video training courses
March 26, 2024 Update: As a brief note today, here are links to my new, free online video training courses:
- Free online “Introduction to Scala 3”
video training course - Free online “Functional Programming”
video training course - Free online “Advanced Scala 3”
video training course
As always, these videos are sponsored by Ziverge software consulting (specializing in Rust, Scala, and ZIO).