Scala Option, Some, None syntax examples

Today I’m sharing some examples of the Scala Option/Some/None syntax. These examples will show how to use an Option for the var fields in a Scala class. Then I’ll show how to set those Option fields, and then get the values from the Option fields.

To get started, we’ll need a little case class to represent an Address:

Veerle Pieters - Duoh! design inspiration

From time to time I visit Veerle Pieters' Duoh! website for some design inspiration. Fonts, colors, art, web design, it's all good. This image is from http://veerle.duoh.com/belgiangraphicdesign/

Some Scala Exception allCatch examples

At the time of this writing there aren’t many examples of the Scala Exception object allCatch method to be found, so I thought I’d share some examples here.

In each example I first show the "success" case, and then show the "failure" case. Other than that, I won’t explain these, but hopefully seeing them in the REPL will be enough to get you pointed in the right direction:

This isn't great

Just reviewed some writing, and then commented on it, “I see where this discussion is necessary, but this isn’t great.”

In this case I was reviewing my own writing. It’s hard to be critical of your own work, so I like to get away from my own work for a day or two, and imagine that someone else wrote it. I then review it like I’m critiquing someone else’s work.

Apple's P/E ratio is 8.9

Apple's P/E ratio is 8.9. Microsoft is 15.8. Dell is 10.4. That's hilarious. Which would you rather own? Which do you think has a better future?

(Remember your Warren Buffett: “Be fearful when others are greedy, and be greedy when others are fearful.”)

How to declare, set, and use Option, Some, and None fields in Scala

Again not much time for a discussion today, but if you’re looking for an example of how to declare, set, and use Option fields in Scala, I hope this source code is helpful:

Three days of snow - Boulder, Colorado

For three or four days it just ... kept ... snowing, and my picture-window view looked like the top photograph. Finally this morning it looked like the bottom photograph. (Near Boulder, Colorado, and the Rocky Mountains.)

Apple's "Here's to the crazy ones" ad, read by Steve Jobs

Apple’s “Here’s to the crazy ones” ad, read by Steve Jobs.

A simple Scala Try, Success, Failure example (reading a file)

Sorry, not much free time these days, so without any discussion, here’s a simple Scala Try/Success/Failure example:

Using find and grep to print lines before and after what you're searching for

I just used this combination of a Unix/Linux find command with a grep command to search for all Scala files under the current directory that contain the string null. This command prints five lines before and after each null line in each file:

Syndicate content