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
This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:
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.
In Scala you can declare a type alias. Typically you do this to create a simple alias for a more complex type.
Using a type alias to simplify a complex type
For example, on this page I note a good example where someone on StackOverflow first defined a type alias named
Row, and then created a second type alias named
Matrix as a list of rows:
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.”
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.
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.”
You want to open a text file and process every character in the file.
If performance isn’t a concern, write your code in a straightforward, obvious way:
Scala FAQ: How can I iterate through each character in a Scala String, performing an operation on each character as I traverse the string?
Depending on your needs and preferences, you can use the Scala
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:
String problem: You need to find the difference between two strings, the common characters between two strings, or the unique characters in a string.
You can perform all of these operations with built-in methods. Use the
diff method to find the differences between two strings:
Scala FAQ: What are the Scala 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: