left

Scala 2.12: Either is biased, implements map and flatMap

While reading the excellent Scala/FP book, Advanced Scala with Cats, I was just reminded that Scala’s Either class was redesigned in Scala 2.12. Prior to 2.12, Either was not biased, and didn’t implement map and flatMap methods. As the image from the book shows, Either is redesigned in 2.12 to include those methods, so it can now be used in Scala for-expressions as shown.

(I write about biasing in my book, Learning Functional Programming in Scala.)

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.

A Scala Either, Left, and Right example (like Option, Some, and None) alvin October 4, 2012 - 4:38pm
Scala: A discussion of the Option/Some/None pattern, and how to use Either/Left/Right instead, when you need to know why something failed.

How to bookmark a directory in the Mac Finder (Part 2)

A very important warning: When you drag your folder over to the PLACES section of the Finder, make sure your image looks like the image shown above, where the folder is clearly going to be placed between two existing folders. If you drag your folder over to the left side, and you're highlighting another folder in the PLACES section, you're going to end up moving your folder to that other folder. That's a great technique for moving your folder from one place to another, but it's not what I'm trying to describe here.

Creating Mac Finder bookmarks (Part 1)

One of the cool things about the Mac Finder is that you can create shortcuts, or bookmarks, to folders that you visit frequently. This is cool, because instead of clicking around to get to your commonly used directories, you can get to them with just one mouse click. That's as fast I can think to make it.