foreach

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.

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 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:

Scala: How to loop over a collection with ‘for’ and ‘foreach’ (plus for loop translation)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.1, “How to loop over a collection with for and foreach (and how a for loop is translated)”

Problem

You want to iterate over the elements in a collection, either to operate on each element in the collection, or to create a new collection from the existing collection.

How to traverse a Map in Scala (for loop, foreach)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.18, “How to Traverse a Map in Scala”

Problem

You want to iterate over the elements in a map.

Solution

There are several different ways to iterate over the elements in a map. Given a sample map:

How to populate Scala collections with a Range

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.25, “How to Populate a Scala Collection with a Range”

Problem

You want to populate a Scala List, Array, Vector, or other sequence with a Range.

Solution

Call the range method on sequence classes that support it, or create a Range and convert it to the desired sequence.

How to create a lazy view on a Scala collection

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.24, “How to Create a Lazy View on a Scala Collection”

Problem

You’re working with a large collection and want to create a “lazy” version of it so it will only compute and return results as they are actually needed.

Scala: How to merge two sequential collections into pairs with ‘zip’

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 10.23, “How to Merge Two Sequential Collections into Pairs with the zip method”

Problem

You want to merge data from two sequential collections into a collection of key/value pairs.

Solution

Use the zip method that’s available to Scala sequential collections to join two sequences into one: