Clojure=> (selections [1 2 3] 1) ((1) (2) (3)) Clojure=> (selections [1 2 3] 2) ((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3)) Clojure=> (selections [1 1 2] 2) ((1 1) (1 1) (1 2) (1 1) (1 1) (1 2) (2 1) (2 1) (2 2))
(defn selections "All the ways of taking n (possibly the same) elements from the sequence of items" [items n] (apply cartesian-product (take n (repeat items))))
Comments top
1 comment(s) for selections.
This is also useful for getting non-unique permutations. The standard permutations function will not repeat elements, but this will. This comes in handy for certain situations.