Created
April 29, 2011 15:30
-
-
Save j5ik2o/948467 to your computer and use it in GitHub Desktop.
findを命令型と関数型で比較
This file contains hidden or 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
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