list

Scala zip and zipWithIndex examples (with Stream)

I’ve known about using the Scala zipWithIndex method for quite some time. I’ve used it in for loops to replace counters, and it works like this:

scala> List("a", "b", "c").zipWithIndex
res0: List[(String, Int)] = List((a,0), (b,1), (c,2))

I learned about using zip with Stream last night while reading Joshua Suereth’s book, Scala In Depth. It works like this:

Scala: Immutable collections of mutable data

Normally I just write about solutions, but I thought I'd take a moment today to write about something else. In this case I just wanted to note that it's possible to create an immutable List of mutable data in Scala. This scenario made me wonder, "What does 'immutable' mean?" Let's take a look.

As a first example, we'll create a Person class that has two fields, and the first field (firstName) can change:

Scala - How to find the unique items in a List, Array, Vector (sequence)

Scala FAQ: How do I find the unique items in a List, Array, Vector, or other Scala sequence?

Use the distinct method. Here's a simple example using a List of integers:

scala> val x = List(1,1,1,2,2,3,3)
x: List[Int] = List(1, 1, 1, 2, 2, 3, 3)

scala> x.distinct
res0: List[Int] = List(1, 2, 3)

As you can see, res0 now contains only the unique elements in the list.

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 try to share examples of the most common List operations (methods).

Five ways to create a Scala List

Scala List class FAQ: How do I create a List in Scala?

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

Convert a Scala array to string with mkString

Scala collections FAQ: How can I convert a Scala array 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.)

Here's a quick array to string example using the Scala REPL:

Scala - How to create a list of alpha or alphanumeric characters

While looking for code on how to create a random string in Scala, I came across this article, which shows one approach for creating a random string. For my brain today, it also shows a nice way to create a list of alpha or alphanumeric characters.

For instance, to create a list of alphanumeric characters, use this approach:

Play Framework form mapping field validators (boolean, text, nonEmptyText, date, email, number, etc.)

The Play Framework scala.play.api.data.Forms object provides form validation helpers (which I've also seen referred to as mappings, data manipulation helpers, and constraints) such as these:

Scala List 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, List, Vector, Seq, etc.)

How to add elements to a List in Scala

Scala List FAQ: How do I add elements to a Scala List?

This is actually a trick question, you can't add elements to a Scala List, it's an immutable data structure, like a Java String.

If you want to add elements to a "list" structure in Scala, use the ListBuffer class instead, like this:

Syndicate content