Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created March 26, 2016 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/6948c7ac79a2ca64df19 to your computer and use it in GitHub Desktop.
Save shigemk2/6948c7ac79a2ca64df19 to your computer and use it in GitHub Desktop.
flatMapのサンプル
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