Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created April 29, 2011 15:30
Show Gist options
  • Save j5ik2o/948467 to your computer and use it in GitHub Desktop.
Save j5ik2o/948467 to your computer and use it in GitHub Desktop.
findを命令型と関数型で比較
case class Car(name: String, color: String)
def find(name: String, cars: List[Car]): Option[Car] = {
for (car <- cars) {
if (car.name == name) {
return Some(car)
}
}
None
}
@Test
def test {
var cars = List(Car("レガシー", "シルバー"), Car("インプレッサ", "ホワイト"), Car("フォレスター", "ブラック"))
val result = find("インプレッサ", cars)
println(result)
val result2 = cars.find(_.name == "インプレッサ")
println(result2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment