immutable

How to drop the first matching element in a Scala sequence

Summary: This blog post shows one way to drop/filter the first matching element from a Scala sequence (Seq, List, Vector, Array, etc.). I don’t claim that the algorithm is efficient, but it does work.

Background

While creating some Scala test code earlier today I had an immutable list of toppings for a pizza, and I got into a situation where I wanted to remove the first instance of a topping.

Strategic Scala Style: The Principle of Least Power alvin March 1, 2017 - 5:52pm

Li Haoyi has a nice blog post titled, Strategic Scala Style: Principle of Least Power.

Starting to write an immutable singly-linked list in Scala

Table of Contents1 - Background: What is a Cons cell?2 - What it might look like in Scala3 - Starting to create my own Cons class4 - My second effort5 - Defining my nil value6 - Defining Cons7 - Replacing the NilCons method bodies8 - Adding a toString method to Cons9 - The complete code at this point10 - I’d really like a :: method11 - Interested?12 - See also

For some examples in my new book on functional programming in Scala I needed to create a collection class of some sort. Conceptually an immutable, singly-linked list is relatively easy to grok, so I decided to create my own Scala list from scratch. This tutorial shows how I did that.

Back to top

Background: What is a Cons cell?

The first time I learned about linked lists was in a language named Lisp. In Lisp, a linked list is created as a series of “Cons” cells. A cons cell is simple, it contains only two things:

What is “Functional Programming”?

Table of Contents1 - Defining “Functional Programming”2 - A working definition of “pure function”3 - Note 1: Higher-Order Functions are a great FP language feature4 - Note 2: Recursion is a by-product5 - Proof: Wikipedia’s FP definition6 - Proof: A wonderful quote from Mary Rose Cook7 - That’s great ... but why immutable values?8 - Summary9 - See also

Note: This is an excerpt from my forthcoming book on Scala and functional programming. (No one has review this text yet. All mistakes are definitely my own.)

Back to top

Defining “Functional Programming”

It’s surprisingly hard to find a consistent definition of functional programming. As just one example, some people say that functional programming (FP) is about writing pure functions — which is a good start — but then they add something else like, “The programming language must be lazy.” Really? Does a programming language really have to be lazy (non-strict) to be FP? (The correct answer is “no.”)

I share links to many definitions at the end of this lesson, but I think you can define FP with just two statements:

The problem of mutating immutable values in FP

When you first start working with immutable values and functional programming (FP), you quickly wonder, “If I can’t mutate a variable value, how do I, well, mutate it? (What can I do?)”

The answer is that you don’t mutate it; you update the fields you need to modify as you copy the old value to a new value. This is okay for simple objects, but when you have deeply nested objects, i.e., cases classes inside of case classes inside of case classes, your code will look like the second part of this image.

Without getting too much into the problem, the solution is something that FP devs call a lens. This image comes from this article that discusses scalaz lenses (and I show a Quicklens example at this URL).

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

Scala programming best practice: Prefer immutable variables (values)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.2, “Scala programming best practice: Prefer immutable variables (values).”

Back to top

Problem

You want to reduce the use of mutable objects and data structures in your code.

Back to top

Solution

Begin with this simple philosophy, stated in the book, Programming in Scala:

“Prefer vals, immutable objects, and methods without side effects. Reach for them first.”

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala best practices (idioms) (from the Scala Cookbook)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is the introduction to Chapter 20, _Idioms_ (Scala best practices).

When I first came to Scala from Java, I was happy with the small things, including eliminating a lot of ;, (), and {} characters, and writing more concise, Ruby-like code. These were nice little wins that made for “a better Java.”

How to use a Queue in Scala (mutable, immutable)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.29, “How to Use a Queue in Scala”

Back to top

Problem

You want to use a queue data structure in a Scala application.

Back to top

Solution

A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and mutable queue. This recipe demonstrates the mutable queue.

You can create an empty, mutable queue of any data type:

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala: How to add, update, and remove elements with immutable Maps

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.16, “How to Add, Update, and Remove Elements with Immutable Maps”

Problem

You want to add, update, or delete elements when working with an immutable map.

Solution

Use the correct operator for each purpose, remembering to assign the results to a new map.

To be clear about the approach, the following examples use an immutable map with a series of val variables. First, create an immutable map as a val: