Three-part Article on Option, Try & Either

I’ve been working for the past couple weeks on writing a three-part series based on last month’s Scala Beginner’s presentation on using Option, Try and Either. I just finished them and published them on

Sichuan-style Braised Cod Recipe

We’ve been trying to experiment with cooking healthier (i.e. lower-calorie) dishes lately, and we wanted to branch out from pan-frying tilapia filets. I remembered how much we use to enjoy a sichuan restaurant in NYC that had a sort of fish-in-broth that was very spicy and also very very delicious.

A traditional dish would include cooking the cod in a nest of thai chilis, but I thought I could do something a little less spicy and a lot easier, so I decided to try stealing some ideas from a Sichuan Stir-Fried Pork in Garlic Sauce recipe I got from my America’s Test Kitchen Twentieth Anniversary Cookbook. (Highly recommended!)

The principle ingredients that deliver the flavor are garlic and chili paste. The latter I bought for that aforementioned stir-fry recipe, worried that I would have an unused chili paste just for one dish, but a few months later I’m almost out! You should be able to find this stuff at your local grocery without going to a specialty shop, but it may take some digging to find it.

If you don’t have some of the ingredients for the braising liquid (mirin, black vinegar, fish sauce) that should be fine. We’ve been playing around with homemade ramen lately, so we’re starting to accumulate some asian condiments.


2 cups prepared white rice

1 lb cod, cut into 4 pieces
salt & pepper
2 tsp corn starch
2 tsp sake (rice wine)

6-8 oz sliced shitake mushrooms
2 celery ribs, cut on bias into 1/4-inch slices

Combine these three in a small bowl:
2 scallions, finely chopped – white parts separated
4 cloves garlic
2 tbsp Asian chili paste

Combine together for the braising liquid:
1 cup chicken or fish stock
1 tbsp cornstarch
2 tsp soy sauce
1 tbsp chinese black vinegar (optional, or substitute 1/2 tbsp rich vinegar and 1/2 tbsp balsamic vinegar)
1 tbsp mirin, if available, or sake
2 tsp fish sauce
1 tsp sesame oil

cilantro (optional for garnish)

Wine Pairing: Sancerre or other White Bordeaux, Loire valley preferred.

Pat the cod dry and sprinkle with some salt and pepper. Mix the sake and corn starch into a little slurry and coat the cod pieces.

Heat 2-3 tbsp vegetable oil to medium-high heat and cook the shitake mushrooms for a 3-5 minutes, then add the celery and cook for another 3-5 minutes. Remove to a bowl and set aside.

Add another 3 tsbp oil into the pan and add the chili paste mixture and cook for 30 seconds until fragrant. Add the cod. (I move some of the chili paste mixture to the top of the cod so it doesn’t scorch). Cook for only a couple minutes, and then flip the cod and cook the other side for a couple more minutes. The surface of the cod should just be a little browned (from the cornstarch) but don’t worry about having it cooked through.

Add the braising liquid and cover. Once the liquid starts to bubble, lower the temperature to medium low. Cook for another 5-10 minutes until the fish is tender. Serve over white rice.

Garnish with cilantro.

If adding green beans make a nice side dish, add a tsp of the chili paste to them near the end.

Scala for Beginners: Option, Try and Either


Use this link to Download the above PDF. The video below can also be accessed via this link.

Meetup lecture video recording.

I have been programming with Scala now for about five years, and I’ve got a confession: until really really recently, there’s a bunch of stuff I never really understood. Now that I look back, I guess it’s understandable. But to be honest, I’m a bit embarrassed.

For example, when I was first learning about Scala, I read that it was a great language because—if you used the right tools—you could write fault-tolerant code that could avoid things like NullPointerExceptions.

I’m talking about Option.

So at times I would use Option if there were scenarios where I might or might not have a value, and I would write inelegant code that would first test whether the item was defined or not, and then would try to extract the value and do something with it. I thought I was just properly following the Scala principles, but things were clunky.

And to be honest, just two weeks ago I was writing some code that was testing and validating a number of scenarios before getting to the core functionality. It was the inner framework to a toolkit that I wanted developers to use, and if a problem happened, it would be really important to catch it and get the appropriate feedback back to the developer. (If I didn’t do that, nobody would ever use my application, or worse: my ears might start burning as they cursed the day they ever decided to give my product a chance!)

I started writing nested code that kept unboxing Try and Option and Either containers, and what felt like it should take take five or six simple lines just looked messy. And I hate messy code!

Long story short, I thought about what I was doing, and rewrote everything so that it involved some nice, clean looking for comprehensions. And then I thought about how I’d first tried using these Option and Try typeclasses. I wished that someone could have sat me down and shown me how to use them properly, way back when about five years ago.

This month, I’ve volunteered to give a presentation in our local Austin Scala Enthusiasts Meetup, and I decided to do another one of my “beginners learning series” with the goal of showing how to use these tools in a way I’d wished someone could have taught it to me.

The code sample for the talk can be found in my Scala Tutorials Github repository (under the “monads” subdirectory)

Advanced Scala: Implicits

First draft of my Implicits lecture.

For about the last 3-4 years I’ve been on a journey that wouldn’t have started if it were for a series of presentations in my local Austin “Scala Enthusiasts” meetup, starting with one where I was first introduced to the concept of “implicits”.

Now, years later, and after reading several books and scratching my head and butting it into walls a whole bunch, I’ve been feeling the desire to create a lecture aimed for the person I was at the very beginning of the journey. You see, I feel like a lot of time I knew I wanted to understand these interesting things… monads and monoids and typeclasses, but I lacked some sort of roadmap that put everything together. I would be grasping at drips and drabs of concepts, but I felt like I was lacking a sort of map that would help me understand where I was headed and where some of the traps and esoteric syntax were all about.

The above video weighs in at 1 hour 25 minutes, and it’s my first attempt to deliver the first of a planned two lectures. I’m planning on re-recording this (having stumbled and mumbled through the first dry run here) and I might break it into a few smaller parts. But rather than letting the “perfect be the enemy of the good” I’m just going to post this now.

Also, here are the slides in PDF form:


Thinking of a Scala Option as a List

Just wanted to show a tiny tidbit of cool and tidy code—and this is one of those things that become intuitive as you start working with lists and flatMap (i.e. Monads, even if you don’t know the term yet) and working toward the wizardry of true FP.

So I’ve got a list (or Seq or similar collection) and I want to append an object to it. Something like:

val name: Option[String] = Some(Dude)
val greetingWords: Seq[String] = List(Hi, there, Dude)

But we’ve got the hitch that the object we want to append is an Option[String]. Maybe it comes from a “getName” function that may not be able to get a name. We could always write the code like this…

val name: Option[String] = Some("Dude")
val greetingWords: Seq[String] = Seq("Hi", "there") ++ { 
  name match {
    case Some(name) => Seq(name)
    case None => Seq.empty[String]

…but that’s ugly and inelegant.

So here’s the elegant way of approaching this: Option[A] can be thought of as a special kind of list that has either one or zero values, depending on whether it’s a Some or a None! So in fact, you can save a lot of the code and add the option as though it were another Seq…

scala> Seq("hi","there") ++ Some("dude")
val res3: Seq[String] = List(hi, there, dude)

scala> Seq("hi","there") ++ None
val res4: Seq[String] = List(hi, there)

Clever, huh?