clojure:子集的排列?

Abe*_*Abe 4 clojure set

我是clojure的新手,正在寻找一个函数来生成子集的排列:

=> (find-subsets 1 #{1 2 3 4})
(#{1} #{2} #{3} #{4})

=> (find-subsets 2 #{1 2 3 4})
(#{1 2} #{1 3} #{1 4} #{2 3} #{2 4} #{3 4})

=> (find-subsets 3 #{1 2 3 4})
(#{1 2 3} #{1 3 4} #{2 3 4})
Run Code Online (Sandbox Code Playgroud)

这样的事情存在吗?如果没有,是否有一个很好的,干净的,惯用的方式来编写函数?

Bla*_*sad 10

看看组合学.它做你需要的:

; all the unique ways of taking n different elements from items
(clojure.math.combinatorics/combinations [1 2 3] 2)
;;=> ((1 2) (1 3) (2 3))
Run Code Online (Sandbox Code Playgroud)

如果它因为你使用集合而不是向量而抱怨,那么在调用之前只需使用vec转换为vector combinations.