sunspots
sunspots

Reputation: 1057

Writing a tuples function that permutes all possible n-tuples

I'm working towards a tuples function, which takes collections and a parameter n. The parameter designates the number of indices the generated vector should have. The function then permutes all possible n-tuples of the elements in the collection.

So far I've been trying to combine functions from tuples.core and math.combinatoris, namely, tuples and permutations.

  (defn Tuples [& args]
        (combo/permutations (tuple args))) 

Example)

input: (0,1) n=3

output: [[0,0,0] [0,0,1] [0,1,0] [1,0,0] [0,1,1] [1,1,0] [1,0,1] [1,1,1]]

Upvotes: 0

Views: 113

Answers (1)

ponzao
ponzao

Reputation: 20934

What you are looking for is clojure.math.combinatorics/selections:

(require '[clojure.math.combinatorics :as c])

(c/selections [0 1] 3)
;=> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))

Upvotes: 1

Related Questions