Posts in the “scala” category

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

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 two different ways:

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

Five ways to create a Scala List

Scala List class FAQ: How do I create a List in Scala? (Also asked as, how do I create and initially populate a List in Scala?)

You can create a Scala List in several different ways, including these approaches:

Two ZIO, Scala CLI, and Scala 3 examples

If you’d like to get started working with the Scala ZIO functional programming (FP) library, here are two little ZIO 101 and ZIO 102 “Hello, world” examples that you can run with Scala CLI and Scala 3.

This post is sponsored by my new book,
Learn Functional Programming Without Fear.

FP: ZIO + Scala CLI example: “Hello, world” 101

First, here’s a complete ZIO “Hello, world” example that shows everything you need to get started using ZIO with Scala CLI:

Creating a Thread (and Runnable) in Scala

I ran into a strange problem this weekend where I noticed a huge difference between the way a Scala Future and a Thread worked for the exact same code. I think I’m pretty aware of the obvious differences between futures and threads, so this really caught me by surprise. If/when I figure out why there was such a difference in behavior for the given problem I’ll post that information here.

A Scala Thread example

While that problem will haunt me for a while, what I really want to post here today is a simple Scala Thread example:

How to convert a Scala Array/List/Seq (sequence) to string with mkString

Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)

A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)