char

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:

How to create a range of characters as a Scala Array alvin June 16, 2018 - 2:51pm

I just noticed this quirk when trying to create an array of characters with the Scala Array.range method:

# works as expected
('a' to 'e').toArray              // Array[Char] = Array(a, b, c, d, e)

# surprise: Array.range always returns Array[Int]
val a = Array.range('a', 'e')     // Array[Int] = Array(97, 98, 99, 100)

I was surprised to see that the Scaladoc for the Array object states that the second example is expected behavior; Array.range always returns an Array[Int]. I suspect this has something to do with a Scala Array being backed by a Java array, but I didn’t dig into the source code to confirm this.

For much more information about arrays, see my Scala Array class examples tutorial.

Scala: Finding the difference, intersection, and distinct characters in a String alvin July 10, 2017 - 12:59pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 1.11, “String Differences, Intersections, and Distinct Characters.”

Problem

In Scala, you need to perform advanced string operations, such as finding the difference between two strings, the common characters between two strings, or the unique characters in a string.

How to process every character in a text file in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.4, “How to process every character in a text file in Scala.”

Problem

You want to open a text file and process every character in the file.

Solution

If performance isn’t a concern, write your code in a straightforward, obvious way:

How to process a Scala String one character at a time (with map, for, and foreach)

Scala FAQ: How can I iterate through each character in a Scala String, performing an operation on each character as I traverse the string?

Solution

Depending on your needs and preferences, you can use the Scala map or foreach methods, a for loop, or other approaches.

The map method

Here’s a simple example of how to create an uppercase string from an input string, using the map method that’s available on all Scala sequential collections:

Scala String differences, intersections, and distinct characters

Scala String problem: You need to find the difference between two strings, the common characters between two strings, or the unique characters in a string.

Solution

You can perform all of these operations with built-in methods. Use the diff method to find the differences between two strings:

Scala data types: bit sizes, ranges, and docs

Scala FAQ: What are the Scala numeric data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

Data Type  Definition

Boolean    true or false

Byte       8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
           -128 to 127

Short      16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
           32,768 to 32,767

Int        32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
           2,147,483,648 to 2,147,483,647

Long       64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
           -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float      32-bit IEEE 754 single-precision float
           1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)

Double     64-bit IEEE 754 double-precision float
           4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative)

Char       16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
           0 to 65,535

String     a sequence of Chars