【发布时间】:2019-11-11 03:29:15
【问题描述】:
我可以看到(1 to 2) 是一个范围。
scala> (1 to 2)
res20: scala.collection.immutable.Range.Inclusive = Range(1, 2)
我可以从这个迭代器中看到集合。
scala> (1 to 3).toSet.subsets
res0: Iterator[scala.collection.immutable.Set[Int]] = non-empty iterator
scala> (1 to 3).toSet.subsets.mkString("\n")
res1: String =
Set()
Set(1)
Set(2)
Set(3)
Set(1, 2)
Set(1, 3)
Set(2, 3)
Set(1, 2, 3)
最后,这是一个扁平化时的集合向量。不平的时候是什么?如何显示?
scala> (1 to 2).flatMap((1 to 3).toSet.subsets(_))
res19: scala.collection.immutable.IndexedSeq[scala.collection.immutable.Set[Int]] = Vector(Set(1), Set(2), Set(3), Set(1, 2), Set(1, 3), Set(2, 3))
【问题讨论】:
-
如果你运行
(1 to 2).map((1 to 3).toSet.subsets),你会得到IndexedSeq[Iterator[Set[Int]]] = Vector(<iterator>, <iterator>)。