How to generate the power set of a set in Scala

前端 未结 8 1954
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 14:41

I have a Set of items of some type and want to generate its power set.

I searched the web and couldn\'t find any Scala code that adresses this specific task.

8条回答
  •  孤城傲影
    2020-12-13 15:33

    Looks like no-one knew about it back in July, but there's a built-in method: subsets.

    scala> Set(1,2,3).subsets foreach println
    Set()
    Set(1)
    Set(2)
    Set(3)
    Set(1, 2)
    Set(1, 3)
    Set(2, 3)
    Set(1, 2, 3)
    

提交回复
热议问题