Scalaのmap高次関数応用
15811 ワード
1つのコード
scala> val c =List("x","y","z")
c:List[String]=List(x, y, z)
scala> c.map(x => x.toUpperCase)
res7:List[String]=List(X, Y, Z)
scala> c.map(_.toUpperCase)
res8:List[String]=List(X, Y, Z)
scala> l.filter(_%2==1)
res10:List[Int]=List(1,3)
scala> l.filter(_%2==1).map(_+10)
res11:List[Int]=List(11,13)
scala> val q=List(l,List(4,5,6))
q:List[List[Int]]=List(List(1,2,3),List(4,5,6))
scala> q.map(x=>x.filter(_%2==0))
res12:List[List[Int]]=List(List(2),List(4,6))
scala> q.map(_.filter(_%2==0))
res13:List[List[Int]]=List(List(2),List(4,6))
scala> q.flatMap(_.filter(_%2==0))
res15:List[Int]=List(2,4,6)