Notes from effective scala
Page content
- Use expressions, not statements(telling the computer what to do)
- Don’t tell it how to
// Don't do this
def findPeopleIn(city: String,
people: Seq[People]): Set[People] = {
val found = new Mutable.HashSet[People]
for (person <- people) {
for (address <- person.addresses) {
if(address.city == city
found.put(person)
}
}
return found
}
// do this
def findPeopleIn(city: String,
people: Seq[People]): Set[People] =
for {
person <- people.toSet[People],
address <- person.addresses
if address.city == city
} yield person
- Mutable safe code
- use Vector instead of Arrays