Clojure: permutation of subsets?

I am new to clojure, looking for a function to generate subset permutations:

=> (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}) 

Is there such a thing? If not, is there a good, clean, idiomatic way to code a function?

+4
source share
1 answer

Take a look at combinatorics . It does what you need:

 ; 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)) 

If he complains because you are using a set instead of a vector, just convert it to a vector with vec before calling combinations .

+10
source

All Articles