Skip to content

Instantly share code, notes, and snippets.

@sdanzig
Last active December 18, 2015 15:09
Show Gist options
  • Save sdanzig/5802364 to your computer and use it in GitHub Desktop.
Save sdanzig/5802364 to your computer and use it in GitHub Desktop.
Demonstration of handling Options retrieved from a Map[String, String] ... More details at: http://sdanzig.blogspot.com/2013/06/the-option-design-pattern.html
def firstToLastNameMap : Map[String,String] = Map(
"beth"->"peterson",
"harry"->"smith",
"john"->"doe"
)
def retrieveLastNameFromMap(firstName: String) = {
firstToLastNameMap.get(firstName)
}
// Sample usage
Console.println("\nRetrieving last name of harry...")
talkAboutValue(retrieveLastNameFromMap("harry"))
Console.println("\nRetrieving last name of ben...")
talkAboutValue(retrieveLastNameFromMap("ben"))
/* Output
Retrieving last name of harry...
value is the String "smith", length of 5
Retrieving last name of ben...
value is None
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment