failure

Functional error handling in Scala

Because functional programming is like algebra, there are no null values or exceptions. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.

Don’t be afraid to fail ~ Nick Foles

“I think the big thing is don’t be afraid to fail. I think in our society today, Instagram, Twitter, it’s a highlight reel. It’s all the good things. And then when you look at it, you think like, wow, when you have a rough day or your life’s not as good as that, like, you’re failing.”

“Failure is a part of life. That’s a part of building character and growing. Without failure, who would you be? I wouldn’t be up here if I hadn’t fallen thousands of times. Made mistakes.”

“We all are human, we all have weaknesses, and I think throughout this, just being able to share that and be transparent. I know when I listen to people speak and they share their weaknesses, I’m listening. Because I can resonate.”

“So I’m not perfect. I’m not Superman. I might be in the NFL, and we might have just won the Super Bowl, but, hey, we still have daily struggles, I still have daily struggles. So that's where my faith comes in, that’s where my family comes in.”

“I think when you look at a struggle in your life, just know that’s just an opportunity for your character to grow. And that’s really just been the message. Simple. If something’s going on in your life and you’re struggling? Embrace it. Because you’re growing.”

~ Nick Foles, Philadelphia Eagles quarterback, and Super Bowl champion

The beginning of a Scala “FileUtils” class

In production code I recommend that you use a good “Files” library like Apache Commons IO, but if you want to create your own Scala FileUtils class, here’s some source code that can help you get started.

First, here’s some code for the FileUtils class (an object, technically):

How to use multiple Futures in a Scala for-comprehension

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

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

Recommended Reading

After I sold Mission Data in 2007, I also sold most of my belongings and drove to Alaska, a state I fell in love with during several vacations many years ago. As a result of that moving process, I sold over 400 books, keeping only the 100 or so “best” books that would fit in my car.

Forced to think about keeping only ~100 books, I learned that my favorite business books are:

Scala best practice: How to use the Option/Some/None pattern

Table of Contents1 - Problem2 - Solution3 - Returning an Option from a method4 - Getting the value from an Option5 - Using Option with Scala collections6 - Using Option with other frameworks7 - Using Try, Success, and Failure8 - Using Either, Left, and Right9 - Discussion10 - Don’t use the get method with Option11 - See Also12 - The Scala Cookbook

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.6, “Scala best practice: How to use the Option/Some/None pattern.”

Back to top

Problem

For a variety of reasons, including removing null values from your Scala code, you want to use what I call the Option/Some/None pattern. Or, if you’re interested in a problem (exception) that occurred while processing code, you may want to return Try/Success/Failure from a method instead of Option/Some/None.

How to open and read text files in Scala alvin June 19, 2015 - 10:57am

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”

Back to top

Problem

You want to open a plain-text file in Scala and process the lines in that file.

Back to top

Solution

There are two primary ways to open and read a text file:

Table of Contents

  1. Problem
  2. Solution
Back to top