Posts in the “scala” category

Scala 3 Unions: Simulating Dynamic Typing with Union Types

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.9, Simulating Dynamic Typing with Union Types.

Problem

When using Scala 3, you have a situation where it would be helpful if a value could represent one of several different types, without requiring those types to be part of a class hierarchy. Because the types aren’t part of a class hierarchy, you’re essentially declaring them in a dynamic way, even though Scala is a statically-typed language.

Solution

In Scala 3, a union type is a value that can be one of several different types. Union types can be used in several ways.

In one use, union types let us write functions where a parameter can potentially be one of several different types. For example, this function, which implements the Perl definition of true and false, takes a parameter that can be either an Int or a String:

Scala collections: Does filter mean retain/keep, remove

As an update before getting into the original article (below), I’ve come to think of the Scala filter method being spelled as the British filtre, and then I think of this acronym, which helps me remember how filter/filtre works: “Find In List Then Retain Each.” So basically filter means retain.

I know that’s a little hokey, but it works for me. And now, here’s the original article:

As part of my personal ongoing battle with the meaning of the filter method in functional programming languages like Scala, I decided to create add a keep method to the Scala List class using the Scala 3 extension method approach, like this:

Scala List/Array/Vector/Seq class filter method examples

The Scala List class filter method implicitly loops over the List you supply, tests each element of the List with the function you supply. Your function must return true or false, and filter returns the list elements where your function returns true.

(Note: Even though I use a List in these examples, the filter method can be used on any Scala sequence, including Array, ArrayBuffer, List, Vector, Seq, etc.)

[toc hidden:1]

Scala List class: 100+ method examples (map, filter, fold, reduce)

This page contains a large collection of examples of how to use the methods on the Scala List class.

Scala List class introduction

The List class is an immutable, linear, linked-list class. It’s very efficient when it makes sense for your algorithms to (a) prepend all new elements, (b) work with it in terms of its head and tail elements, and (c) use functional methods that traverse the list from beginning to end, such as filter, map, foldLeft, reduceLeft.

Yvonne De Carlo in The Ten Commandments (and more)

I recently watched the movie, The Ten Commandments, and when I saw Sephora, I thought, “She looks familiar.”

It turns out she’s played by Yvonne De Carlo. She’s the actress who’s portrayed on my Learn Scala 3 book cover, which is based on the 1950 movie, Buccaneer’s Girl, which she starred in. (Until this, I thought I only knew her as Lily Munster on The Munsters.)

Scala - How to declare an empty immutable Set (SortedSet)

I just saw this code that shows how to declare an empty immutable SortedSet in Scala, and wanted to save it as a helpful snippet here:

/**
 * The node ring gossipped that contains only members that are Up.
 */
var nodes: immutable.SortedSet[Address] = immutable.SortedSet.empty

Scala immutable Map class: methods, examples, and syntax

This page contains a large collection of examples of how to use the Scala Map class. There are currently well over 100 examples.

A Scala Map is a collection of unique keys and their associated values (i.e., a collection of key/value pairs), similar to a Java Map, Ruby Hash, or Python dictionary.

Scala packaging and import examples

This article is a collection of Scala packaging and import examples. I created most of these examples when I was writing the 1st Edition of the Scala Cookbook. I share them here without much discussion, but for more examples and discussion, please check out the Cookbook.

Packages imported by default

By default, three packages are implicitly imported for you:

  • java.lang._
  • scala._
  • scala.Predef._

Passing an anonymous function to SwingUtilities.invokeLater (in Scala)

As a quick note, if you ever need to call the invokeLater of the Java SwingUtilities class in Scala, you can pass it an anonymous function (lambda) like this:

SwingUtilities.invokeLater(() => {
    // your gui-updating code here, such as:
    for (c <- newsControllers) c.updateContent()
})

(I haven’t worked with Java 8 lambdas too much, but I assume that the Java lambda syntax is similar to that.)

Book: Learn Functional Programming The Fast Way! (FP for OOP developers)

LATE MARCH, 2023: You can now download a large, free preview of the book. See the “Free Preview” section below.

MARCH, 2023: This book was previously named, Learn Functional Programming Without Fear, but I have renamed it to Learn Functional Programming The Fast Way!. I think this name is more reflective of the ZIO and Cats Effect libraries being easier to learn than ever before (without having to know category theory), and the name is also consistent with my other book, Learn Scala 3 The Fast Way!.

FEB., 2023: Learn Functional Programming Without Fear is now one of my best-selling books on Gumroad.com (see the link below).

NOV., 2022: My new book, Learn Functional Programming Without Fear, is currently an Amazon Java and functional programming #1 new release. The book is now available in three formats:

PDF Format
$10 (USD) on Gumroad.com

Learn Functional Programming The Fast Way! (PDF Version)

Paperback
$30 on Amazon

Learn Functional Programming The Fast Way (Paperback)

Kindle
$10 on Amazon

Learn Functional Programming The Fast Way! (Kindle Edition)