yield

Scala for/yield examples (for-loop and yield examples)

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will be helpful to know how it works. Here's a statement of how the yield keyword works in for loops, based on the documentation in the book, Programming in Scala:

How to use multiple Futures in a Scala for-comprehension

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

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 create a Scala “for comprehension” (for/yield loop)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.4, “How to create a "for comprehension" (for/yield loop).”

Problem

You want to create a new collection from an existing collection by applying an algorithm (and potentially one or more guards) to each element in the original collection.

Solution

Use a yield statement with a for loop and your algorithm to create a new collection from an existing collection.

How to transform one Scala collection to another with the ‘map’ function (method)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.14, “How to Transform One Scala Collection to Another With the map function (method)”

Problem

Like the previous recipe, you want to transform one collection into another by applying an algorithm to every element in the original collection.

How to transform Scala collections with for/yield

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.13, “How to Transform One Scala Collection to Another with for/yield”

Back to top

Problem

You want to create a new collection from an existing collection by transforming the elements with an algorithm.

Back to top

Solution

Use the for/yield construct and your algorithm to create the new collection. For instance, starting with a basic collection:

Table of Contents

  1. Problem
  2. Solution
Back to top

Scala for loop syntax examples (including yield and guards)

Scala FAQ: Can you share some examples of the Scala for loop syntax?

Sure. I'm going to start with a comparison to Java for loops, because that's what I was just thinking about.

In Java you might write a for loop with a counter like this:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}

The equivalent for loop in Scala looks like this:

for (i <- 1 to 10) {
  println(i)
}

(The use of parentheses isn’t necessary in either example, but most for loops will be longer.)