Created
March 26, 2016 14:02
-
-
Save shigemk2/6948c7ac79a2ca64df19 to your computer and use it in GitHub Desktop.
flatMapのサンプル
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 Student(name: Option[String], age: Option[Int], grade: Option[Int]) | |
val list = List( | |
Some(Student(Some("Steve"), Some(15), Some(1))), | |
Some(Student(Some("Chad"), Some(16), Some(2))), | |
Some(Student(Some("Shigeru"), Some(40), Some(1))), | |
Some(Student(Some("Chloé"), Some(18), Some(3))) | |
) | |
println(list.flatMap(_.get.name)) | |
println(list.flatMap(_.get.name.get.toUpperCase)) | |
println(Seq(Seq(1,2,3), Seq(4), Seq(5, 6)) flatMap { x => x }) | |
println(Seq(Seq(1,2,3), Seq(4), Seq(5, 6)) flatMap { x => 10 +: x }) | |
println(Seq(Seq(1,2,3), Seq(4), Seq(5, 6)) flatten) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment