Created
September 22, 2015 21:51
-
-
Save polyglotpiglet/236f3b1ba6d901116f85 to your computer and use it in GitHub Desktop.
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
// original (ew) | |
class Person(val name: String, val department: String) | |
var people = Array(new Person(“Jones”, “Marketing”), new Person(“Smith”, “Engineering”)) | |
var engineer: Person = null | |
var index = 0 | |
while (index < people.length) { | |
if (people(index).department == “Engineering”) engineer = people(index) index = index + 1 | |
} | |
println(engineer.name + “ is an engineer”) | |
// revised | |
case class Person(name: String, department: String) | |
val people = Array(Person("Jones", "Marketing"), Person("Smith", "Engineering")) // why not make it a set | |
// maybe something like this: | |
people.filter(_.department == "Engineering").foreach{ p => println(s"${p.name} is an engineer")} | |
// or maybe like this: | |
people.foreach{ case Person(name, dept) => if (dept == "Engineering") println(s"$name is an engineer")} // i like this better |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment