generator

ScalaCheck custom generator examples

Table of Contents1 - Custom generators2 - Built-in ScalaCheck generators3 - How to use ScalaCheck generators4 - More ScalaCheck generators

Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.

Back to top

Custom generators

This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:

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

How to Enable the Use of Multiple Generators in a ‘for’ Expression

One cool thing about for expressions is that you can use multiple generators inside of them. This lets you do some nice analytics when you have some interesting data relationships.

For instance, suppose you have some data like this:

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

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:

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 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 loop over a Scala collection with a ‘for’ loop

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

Back to top

Problem

You want to loop over the elements in a collection using a for loop, possibly creating a new collection from the existing collection using the for/yield combination.

Back to top

Solution

You can loop over any Traversable type (basically any sequence) using a for loop:

Table of Contents

  1. Problem
  2. Solution
Back to top