1.2.0 permalink Arrow_down_16x16
  • (selections items n)
All the ways of taking n (possibly the same) elements from the sequence of items

1 Example top

  • 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))
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/combinatorics.clj:127 top

(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))))
Vars in clojure.contrib.combinatorics/selections: apply defn repeat take
Used in 0 other vars

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.

Log in to add a comment.