Given a collection I want to iterate through all pairs in a collection. Example
(all-pairs seq)
(all-pairs '(a b c d)) => ([a b] [a c] [a d] [b c] [b d] [c d]))
Here is my idea
(defn all-pairs [coll]
(for [ [idx elmt] (indexed coll)
other-elmt (subvec coll (inc idx))]
(vector elmt other-elm)))
But it doesn't feel idiomatic
How about:
(use 'clojure.contrib.combinatorics)
(vec (map vec (combinations '(a b c d) 2)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With