Created
March 24, 2015 00:33
-
-
Save tancnle/3a34c44347dbb841ea97 to your computer and use it in GitHub Desktop.
Scala For Expressions
This file contains 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
case class Mascot(name: String) | |
case class SportTeam(mascot: Mascot) | |
case class City(sportTeams: List[SportTeam]) | |
case class State(cities: List[City]) | |
case class Country(states: List[State]) | |
def getAllMascots(country: Country): List[Mascot] = { | |
for { | |
states <- country.states | |
cities <- states.cities | |
sportTeams <- cities.sportTeams | |
} yield sportTeams.mascot | |
// country.states.flatMap { state => | |
// state.cities.flatMap { city => | |
// city.sportTeams.map { sportTeam => | |
// sportTeam.mascot | |
// } | |
// } | |
// } | |
} | |
val mascot = Mascot("Donald Duck") | |
val sportTeam = SportTeam(mascot) | |
val city = City(List(sportTeam)) | |
val state = State(List(city)) | |
var country = Country(List(state)) | |
getAllMascots(country) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment