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:

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 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: 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:

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:

  1. Multiply the number by itself
  2. Call the Java Math.pow function or the scala.math.pow function

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.

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: