Skip to content

Instantly share code, notes, and snippets.

@sadache
Created October 20, 2010 07:57
Show Gist options
  • Save sadache/635990 to your computer and use it in GitHub Desktop.
Save sadache/635990 to your computer and use it in GitHub Desktop.
Simple example of the Option type in Scala
object CityDictionary{
def getMeACity(cityCode:String) :Option[City]={
if(cityCode=="75") Some(City("Paris","75"))
else None
}
case class City(name: String,code:String)
}
object NPEApplication {
def getGreeting(cityName:String):Option[String] ={
if(cityName=="Paris") Some("Welcome in Paris!") else None
}
def getCityName(cityCode:String):String ={
(for( c <- CityDictionary.getMeACity(cityCode);
g <- getGreeting(c.name)) yield g).getOrElse("NotFound!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment