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.

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.

How to create and populate Kotlin lists (initialize mutable and immutable lists)

If you ever need to create and populate a Kotlin list, I can confirm that these approaches work for an immutable and mutable lists:

// fill an immutable list
val doubles = List(5) { i -> i * 2 }

// fill a mutable list of ten elements with zeros
val ints = MutableList(10) { 0 }

The Kotlin REPL shows how these approaches work:

Kotlin functions to create Lists, Maps, and Sets

Table of Contents1 - Kotlin Arrays2 - Kotlin List functions3 - Kotlin Map functions4 - Kotlin Set functions5 - Summary: Kotlin List, Map, and Set creation functions

With Kotlin you can create lists, maps, and sets with standard functions that are automatically in scope. Here are those functions.

How to create an empty Scala Map

If you need to create an empty Map in Scala, the following examples show how to create empty immutable and mutable maps.

An empty immutable Map

You can use these approaches to create an empty immutable Scala Map:

val a = Map[Int, String]()
val a = Map.empty[Int, String]

Here’s what they look like in the REPL:

Scala idiom: Prefer immutable code (immutable data structures) alvin February 7, 2018 - 10:50pm

(I originally wrote this blog post in 2012, and it seems like it has held up well over time.)

One great thing I’ve learned from Scala and functional programming over the last few months is this: 

Make your variables immutable, unless there’s a good reason not to.

“Immutable” means that you can’t change (mutate) your variables; you mark them as final in Java, or use the val keyword in Scala. More important than you not changing your variables is that other programmers can’t change your variables, and you can’t change theirs.

Erlang has single-assignment variables

“Erlang has single-assignment variables. As the name suggests, they can be given a value only once. If you try to change the value of a variable once it has been set, you’ll get an error.”

(“Single-assignment variables” are the same as val fields in Scala. Using them can make your code more like algebra.)

In Erlang, it’s OK to mutate state within an individual process alvin June 18, 2017 - 11:19am

“In Erlang (Akka), it’s OK to mutate state within an individual process (actor), but not for one process to tinker with the state of another process.”