Skip to content

Instantly share code, notes, and snippets.

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