Created
April 29, 2011 15:09
-
-
Save j5ik2o/948435 to your computer and use it in GitHub Desktop.
filterを命令型と関数型で比較
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
import collection.mutable.ListBuffer | |
// 命令型で書いた場合 | |
val filtered = new ListBuffer[Int] | |
val numbers = List(1,2,3,4,5) | |
for(n <- numbers){ | |
if (n % 2 == 0){ | |
filtered += n | |
} | |
} | |
println(filtered.toList) | |
// 関数型で書いた場合 | |
val numbers2 = List(1,2,3,4,5) | |
val filtered2 = numbers2.filter(_ % 2 == 0) | |
println(filtered2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment