try

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:

Two thoughts of the day

Two thoughts of the day:

Nobody owes you anything. I’m often surprised to hear people tell stories about how they’re butthurt because they feel like someone owes them something. (Typically they think friends and family members should owe them something.)

The only time you really lose is when you don’t try. A lot of people have interesting ideas, but never do anything with them. I think I lost nine games as a baseball pitcher in high school, but I won a lot more games than that, and it would have been a much bigger loss if I thought, “Gosh, I could be a great pitcher if only someone gave me a chance.”

A Scala method to run any block of code slowly

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that (without a catch clause), so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)

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.

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):

Scala: Handling nested Options with flatMap and for

Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one Option instance contains one or more other Options, this article may be helpful.

There are times when you’re creating your domain model when it makes sense to use optional fields in your case classes. For instance, when you model an Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:

I’m saddened by people are afraid to make a mistake

I’m saddened by people who are so afraid of making a mistake that they come up with a million different reasons as excuses to justify why something can’t be done. They always say, “I would do XYZ, but ...”

All I can think to say to them is, “Quit thinking and just do it.” Or, as Cher said in Moonstruck, “Snap out of it!”

Really, what’s the worst thing that’s going to happen? You’re going to die? Well, I have news for you, no matter what you do, you’re going to die anyway. (I’ve gone unconscious seven times over the last couple of years, and believe me, at that point there’s nothing you can do about it.)