scala cookbook

On deleting a chapter from the Scala Cookbook

In an effort to “Get rid of the crap,” I asked my editor if we could delete an entire chapter from the Scala Cookbook, and she said yes. I didn’t know if they’d allow that since the advance they paid me is based on a proposal of X chapters, but they’re all-in on just trying to create a good book, which is nice.

This is a page from my book, “A Survival Guide for New Consultants”

More information

As I finish editing this book in January, 2014, it’s a busy time. In addition to releasing this book, I just finished writing a 700+ page computer programming book for O’Reilly named Scala Cookbook.

A funny thing about the Scala Cookbook: I wrote it on a whim, as a dare, wondering, “Am I good enough to write this book, on my own?” I doubted myself for a little while, but then overcame my fears and sent an email to the people at O’Reilly. As I’ve said throughout this book, all you are is attitude.

Polar bear photos (mom and cub) alvin July 31, 2017 - 11:20am

Photos of mom and cub polar bears. I hoped O’Reilly would use these for the cover of the Scala Cookbook — because of the whole “scalable” thing, and to help raise awareness about climate change — but whoever puts images on their book covers had something else in mind.

How to split strings in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.3, “How to Split Strings in Scala.”

Problem

You want to split a string into parts based on a field separator, such as a string you get from a CSV or pipe-delimited file.

Solution

Use one of the split methods that are available on String objects:

How to create multiline strings in Scala alvin July 9, 2017 - 6:05pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.2, “How to Create Multiline Strings in Scala.”

Problem

You want to create multiline strings within your Scala source code, like you can with the “heredoc” syntax of other languages.

Solution

In Scala you create multiline strings by surrounding your text with three double quotes:

How to test String equality in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 1.1, “Testing String Equality in Scala.”

Problem

When using Scala, you want to compare two strings to see if they’re equal, i.e., whether they contain the exact same sequence of characters.

Solution

In Scala you compare two String instances with the == operator. Given these strings:

How I came to write the Scala Cookbook

The funny thing about writing the Scala Cookbook is that it started as a whim. I was just about to leave for a vacation at the beach, and right before I turned off the computer, a thought flashed in my mind, “I should contact the people at O’Reilly about writing a cookbook for Scala.” I then had a doubt that they would actually do it, but I applied the “What the heck” rule — i.e., “What the heck, what do I have to lose?” — and sent the email.

I dug around the internet for a few minutes, found the correct O’Reilly email address, sent them a message, turned off the computer, and drove to the beach. While I was at the beach the publisher wrote and said, “Love it, send me a full proposal!”

So if you’re thinking about doing something, but are afraid or uncertain about doing it ... apply the “What the heck” rule, and give it a shot. :)

Last draft of the Scala Cookbook alvin May 24, 2017 - 8:43am

On May 24, 2013, I finished with the last hardcopy chapters of the Scala Cookbook. I put all of the chapters next to the paper shredder as a way to show what I had just done. The final edits would be finished with a copywriter over the next several weeks, and I signed off on the final edition while I was at Virginia Beach.

Writing a book is an adventure. To begin with it is a toy and an amusement.

“Writing a book is an adventure. To begin with it is a toy and an amusement. Then it becomes a mistress, then it becomes a master, then it becomes a tyrant. The last phase is that just as you are about to be reconciled to your servitude, you kill the monster and fling him to the public.”

~ Winston Churchill

A Java tuple class (Tuple2 or Pair, if you prefer)

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

Tuple<String, Integer> t = new Tuple<>("age", 41);

If you’re comfortable with generics, the Java implementation of a Tuple class like this is simple: