Blogs

A Scala case class copy method example

Scala case class FAQ: When you create a case class in Scala, a copy method is generated for your case class. What does this copy method do?

In short, it lets you make a copy of an object, where a "copy" is different than a clone, because with a copy you can change fields as desired during the copying process.

To demonstrate this, let's create an Employee class as a case class:

The 'using' control structure in Beginning Scala (by David Pollak)

In David Pollak's book, Beginning Scala, he introduces a using control structure that is helpful in several ways. First, it can be used to automatically close any resource that has a close method. Second, it's a great example of how to create a custom control structure in Scala.

Introduction

Here's the source code for the using control structure he created:

Jonathan Ive design interview quotes

This article contains a collection of quotes on design from Jonathan Ive (or "Jony Ive", as Steve Jobs called him), Apple designer.

For those who don't know much about him, Mr. Ive is credited with designing almost every Apple product since 1997. Given that very long string of success, I became interested in what Mr. Ive has to say, and to that end, here's a collection of Jonathan Ive design interview quotes I've gathered over the last few years.

Scala 'unreachable code due to variable pattern' warning message

Scala match FAQ: Why am I getting an "unreachable code due to variable pattern" warning on my match/case statement when compiling my Scala code?

The usual reason for getting this error message is that you try to use an existing variable name inside a Scala match expression. For instance, if you try to create a match expression like this:

Writing the Scala Cookbook for O'Reilly - February, 2013

I've been keeping notes about the process of writing the Scala Cookbook for O'Reilly, and I thought I'd share a few of those thoughts here today (early February, 2013).

Scala break and continue examples

Scala FAQ: Can you share some examples of how to implement break and continue functionality in Scala?

Sure. The example Scala code below shows both a break and a continue example. As you can see from the import statement, it uses the code in the Scala util.control.Breaks package.

To understand how this works, let's first look at the code, and then the output. First, here's the code:

Scala for loop syntax examples (including yield and guards)

Scala FAQ: Can you share some examples of the Scala for loop syntax?

Sure. I'm going to start with a comparison to Java for loops, because that's what I was just thinking about.

In Java you might write a for loop like this:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}

The equivalent for loop in Scala looks like this:

for (i <- 1 to 10) {
  println(i)
}

(The use of parentheses isn't necessary in either example, but most for loops will be longer.)

Scala - How to count the number of occurrences of a character in a String

Scala String FAQ: How can I count the number of times (occurrences) a character appears in a String?

Use the count method on the string, using a simple anonymous function, as shown in this example in the REPL:

scala> "hello world".count(_ == 'o')
res0: Int = 2

There are other ways to count the occurrences of a character in a string, but that's very simple and easy to read.

Wanted: Scala Cookbook reviewers

UPDATE: I originally posted this article in January, 2013, and it's now mid-February, 2013, and we're no longer looking for reviewers. I've only kept this page here so people won't get 404 errors.

OLD CONTENT:

Interested in being a reviewer for the Scala Cookbook?

Scala Map examples - mapping month numbers to names

Nothing too earth shattering here today, but if you need an example of the Scala Map 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:

Syndicate content