sequence

This is a page from my book, Functional Programming, Simplified

How to Enable Filtering in a Scala `for` Expression

Next, let’s see if we can use a filtering clause inside of a for expression with the Sequence code we have so far.

Trying to use a filtering expression

When I paste the current Sequence class and this code into the Scala REPL:

val ints = Sequence(1,2,3,4,5)

val res = for {
    i <- ints
    if i > 2
} yield i*2

I see the following error message:

This is a page from my book, Functional Programming, Simplified

How To Make Sequence Work as a Single Generator in a `for` Expression

Getting Sequence to work as a generator in a simple for loop was cool, but does adding foreach let Sequence also work when I add yield? Let’s see.

When I paste this code into the REPL:

val ints = Sequence(1,2,3)

for {
    i <- ints
} yield i*2

I see this error message:

This is a page from my book, Functional Programming, Simplified

How to Make Sequence Work in a Simple Scala `for` Loop

So far I have this Sequence class:

case class Sequence[A](initialElems: A*) {

    private val elems = scala.collection.mutable.ArrayBuffer[A]()

    // initialize
    elems ++= initialElems

}

With that code I can create new Sequence instances like this:

val strings = Sequence("a", "b", "c")
val nums = Sequence(1, 2, 3, 4, 5)

Next, I’ll modify Sequence so I can use it as a generator in a for loop.

This is a page from my book, Functional Programming, Simplified

How to Create a Scala Sequence Class to be Used in a ‘for’ Expression

The best way I know to demonstrate how the Scala for expression works is for us to build our own collection class.

To keep things simple I’m going to create a custom class as a “wrapper” around an existing Scala collection class. The reason for this is that I want you to focus on the effects that writing map, flatMap, withFilter, and foreach methods have on how the class works in a for expression — not on writing the gory internals of a collection class.

How to shuffle (randomize) a list in Scala

As a quick note today, to shuffle/randomize a list in Scala, use this technique:

scala.util.Random.shuffle(List(1,2,3,4))

Here’s what this approach looks like in the Scala REPL:

Scala ‘for loop’ examples and syntax

Table of Contents1 - Example data structures2 - Basic for-loop examples3 - Using generators in for-loops4 - Scala for-loop generators with guards5 - Scala for/yield examples (for-expressions)6 - Scala for-loop counters (and zip, zipWithIndex)7 - Using a for loop with a Scala Map8 - Multiple futures in a for loop9 - foreach examples10 - Summary

Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples.

This page is a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala for loop examples — technically called a for-comprehension or for-expression — I hope these examples are helpful.

 

 

How to list subdirectories beneath a directory in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.10, “How to list subdirectories beneath a directory in Scala.”

Problem

You want to generate a list of subdirectories in a given directory.

Solution

Use a combination of the Java File class and Scala collection methods:

How to use pattern matching in Scala match/case expressions

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.11, “How to use pattern matching in Scala match expressions.”

Problem

You need to match one or more patterns in a match expression, and the pattern may be a constant pattern, variable pattern, constructor pattern, sequence pattern, tuple pattern, or type pattern.

How to use a Scala `for` loop with embedded `if` statements (guards)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.3, “How to use a 'for' loop with embedded 'if' statements (guards).”

Problem

You want to add one or more conditional clauses to a for loop, typically to filter out some elements in a collection while working on the others.

Solution

Add an if statement after your generator, like this:

How to use Scala ‘for’ loops (expressions) with multiple counters (multi-dimensional arrays)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.2, “How to use Scala for loops with multiple counters.”

Problem

You want to create a loop with multiple counters, such as when iterating over a multi‐dimensional array.

Solution

You can create a for loop with two counters like this: