Skip to content

Instantly share code, notes, and snippets.

@tancnle
Created March 24, 2015 00:33
Show Gist options
  • Save tancnle/3a34c44347dbb841ea97 to your computer and use it in GitHub Desktop.
Save tancnle/3a34c44347dbb841ea97 to your computer and use it in GitHub Desktop.
Scala For Expressions
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