1.2.0 permalink Arrow_down_16x16
  • (combinations items n)
All the unique ways of taking n different elements from items

1 Example top

  • Clojure=> (combinations '(a b c) 2)
    ((a b) (a c) (b c))
    Clojure=> (combinations '(a b c d e) 3)
    ((a b c) (a b d) (a b e) (a c d) (a c e) (a d e) (b c d) (b c e) (b d e) (c d e))
    
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:90 top

(defn combinations
  "All the unique ways of taking n different elements from items"
  [items n]      
  (let [v-items (vec (reverse items))]
    (if (zero? n) (list ())
	(let [cnt (count items)]
	  (cond (> n cnt) nil
		(= n cnt) (list (seq items))
		:else
		(map #(map v-items %) (index-combinations n cnt)))))))
Vars in clojure.contrib.combinatorics/combinations: = > cond defn let list map seq zero?
Used in 0 other vars

Comments top

No comments for combinations. Log in to add a comment.