scala学习笔记:集合

scala> 1 to 10
res9: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> List(1,2,3,4)
res10: List[Int] = List(1, 2, 3, 4)

scala> (1 to 10).map(_*3).filter(_%2==1).reduce(_+_)
res8: Int = 75

试一试map

scala> (1 to 9).map((0 to _))
res5: scala.collection.immutable.IndexedSeq[scala.collection.immutable.Range.Inclusive] = Vector(Range(0, 1), Range(0, 1, 2), Range(0, 1, 2, 3), Range(0, 1, 2, 3, 4), Range(0, 1, 2, 3, 4, 5), Range(0, 1, 2, 3, 4, 5, 6), Range(0, 1, 2, 3, 4, 5, 6, 7), Range(0, 1, 2, 3, 4, 5, 6, 7, 8), Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

scala> (1 to 9).flatMap((0 to _))
res6: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

你可能感兴趣的:(scala学习笔记:集合)