Skip to content

Instantly share code, notes, and snippets.

@j5ik2o j5ik2o/gist:948467
Created Apr 29, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.