numbers

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:

Scala for Java devs: Everything in Scala is an object

The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).

More sales tips alvin August 1, 2017 - 9:59am

The most important thing I can tell you about selling is to put down this book, and go buy a copy of How I Raised Myself From Failure to Success in Selling, by Frank Bettger. It’s an old book, but it’s the single most important book on “selling” I have ever read.

In addition to that suggestion, here are a few other important sales tips I know.

How to compare floating-point numbers in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 2.5, “Comparing Floating-Point Numbers in Scala.”

Problem

You need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be.

MacOS softwareupdate command (how to ignore updates)

I just learned that MacOS has a softwareupdate command, and further learned that it has a --ignore option, which may or may not let you ignore useless updates. For example, my Mac prompts me daily to update Keynote, Numbers, and Pages, which I rarely (rarely!) use, so I don’t want to bother updating them. I’m hoping the a softwareupdate command will help me with this.

How to create a Range, List, or Array of numbers in Scala

Scala FAQ: How can I create a range, list, or array of numbers in Scala, such as in a for loop, or for testing purposes?

Solution

Use the to method of the Int class to create a Range with the desired elements:

scala> val r = 1 to 10
r: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

You can also set the step with the by method:

Scala Map class examples - mapping month numbers to names

Nothing too earth shattering here today, but if you need an example of the Scala Map class syntax (how to create a Scala Map), or just want to copy and paste a map of month names to numbers (or numbers to names), I hope the following code is helpful: