for comprehension

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

A Quick Review of Scala’s ‘for’ Expressions

“The owls are not what they seem.”

The “Log Lady” in Twin Peaks

Goals

The goal of this lesson is to review at a high level how for loops work in Scala. This is necessary because Scala/FP developers take advantage of advanced Scala for loop features.

As an example of what I mean, the goal of the next few lessons is to explain what’s happening in this for loop:

STRef, or StateRef (using a State monad) alvin May 30, 2017 - 2:13pm

Here’s a link to a page by James Earl Douglas that I don’t quite understand yet, but also don’t want to forget. Here’s his intro to the problem, and then the image shows his solution.

Problem: You have a mutable variable (a var in Scala) that is both read from and written to outside of a tightly-scoped block.

Solution: Remodel the block as functions that take an initial value of the variable, and return both the final value of the variable and the original return value of the expression.

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 multiple generators in Scala ‘for’ expressions (loops)

A cool thing about Scala for loops — what I’ll more-accurately call for expressions in this article — is that you can have multiple generators. What’s also very cool about them is how they work.

For example, imagine that you have these two values:

val nums = Seq(1,2,3)
val letters = Seq('a', 'b', 'c')

An interesting question then becomes, “What is the type of res in this expression?”:

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 process multiple Option values in a Scala ‘for’ loop

My last edits to the Scala Cookbook were in June, 2013, and after all this time there aren’t many things I wish I had added to the Cookbook. Yesterday I ran into one thing that I don’t think I included in the Cookbook: How to process multiple Option values in a Scala for loop (for comprehension). Here’s a quick look at how to do this.

For the impatient

For those who just want to see a for comprehension that processes multiple input Option values, here you go:

The Scala ‘for’ loop translation scheme

If you're interested in the details of the translation scheme of a Scala for loop (for comprehension), here's a quick look at how a for loop is translated into, well, other code.

A simple Scala for loop

In a first example, we'll start with the following Scala class:

class Main {
  def foo { for(i <- 0 to 10) println(i) }
}

Next, I compile this class from the command line like this: