或者不用标准库,自己写一个,和python基本一样:
//
scala> def subsets[A](xs: Seq[A]): Seq[Seq[A]] =
| def select(es: Seq[A], n: Int): Seq[Seq[A]] = es match
| case Seq() => if n == 0 then Seq(Nil) else Seq()
| case h :+ t => select(es.init, n) ++ select(es.init, n-1).map(s => s :+ t)
| (1 to xs.size).flatMap(i => select(xs, i))
|
def subsets[A](xs: Seq[A]): Seq[Seq[A]]
scala> subsets("abcd")
val res2: Seq[Seq[Char]] = Vector(List(a), List(b), List(c), List(d), List(a, b), List(a, c), List(b, c), List(a, d), List(b, d), List(c, d), List(a, b, c), List(a, b, d), List(a, c, d), List(b, c, d), List(a, b, c, d))
【 在 bsxfun 的大作中提到: 】
: Scala:
: [code=scala]
: //
: ...................
--
FROM 223.166.244.*