Created
October 20, 2010 07:57
-
-
Save sadache/635990 to your computer and use it in GitHub Desktop.
Simple example of the Option type in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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