How to create all possible combinations from the elements of a list?

前端 未结 4 1394
醉话见心
醉话见心 2021-02-01 02:07

I have the following list:

List(a, b, c, d, e)

How to create all possible combinations from the above list?

I expect something like:

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 02:30

    def powerset[A](s: Set[A]) = s.foldLeft(Set(Set.empty[A])) { case (ss, el) => ss ++ ss.map(_ + el) }
    

    Sounds like you need the Power set.

提交回复
热议问题