开发者

Idiomatic way to iterate through all pairs of a collection in Clojure

开发者 https://www.devze.com 2023-01-22 08:44 出处:网络
Given a col开发者_运维问答lection I want to iterate through all pairs in a collection. Example

Given a col开发者_运维问答lection 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)))


Lazy, and relatively fast.

(defn all-pairs [coll]
  (when-let [s (next coll)]
    (lazy-cat (for [y s] [(first coll) y])
              (all-pairs s))))

(defn all-pairs [coll]
  (let [x (first coll) xs (next coll)]
    (when xs
      (lazy-cat
       (map (fn [y] [x y]) xs) 
       (all-pairs xs)))))

(all-pairs [1 2 3 4]) ;; => ([1 2] [1 3] [1 4] [2 3] [2 4] [3 4])

(all-pairs '(a b c d)) ;; => ([a b] [a c] [a d] [b c] [b d] [c d])


(defn all-pairs [coll]
  (loop [[x & xs] coll
         result []]
    (if (nil? xs)
      result
      (recur xs (concat result (map #(vector x %) xs))))))


May I suggest:

(defn all-pairs [sq] (for [i sq j sq] [i j]))

EDIT: Clearly I misread the question; since you only want distinct unduplicated pairs, we can still use this approach if a natural ordering exists on whatever domain you're calling this function on.

(defn all-pairs [sq] (filter #(< (first %) (second %)) (for [i sq j sq] [i j])))

EDIT 2

Also:

(defn all-pairs [sq]
    (partition 2 (flatten (map (fn [sqi] (map #(vector %1 %2) sq sqi))
                   (take-while not-empty (iterate rest (rest sq)))))))


If you want to write your own combinations function in "academic style," you can try

(defn comb [xs m]
  (cond
    (= m 0) (list ())
    (empty? (seq xs)) ()
    :else (let [x (first xs)
                xs (rest xs)]
            (concat
             (map #(cons x %) (comb xs (- m 1)))
             (comb xs m)))))

and then apply it to your problem as follows

(map vec (comb '(a b c d) 2))
([a b] [a c] [a d] [b c] [b d] [c d])


A simple recursive version that should do what you want:

(defn all-pairs [coll]
  (let [x (first coll)
        xs (rest coll)]
    (if (empty? xs) 
      nil
      (concat 
        (map (fn [y] [x y]) xs) 
        (all-pairs xs)))))


Not the fastest solution, but:

; handy helper function
(defn tails [v]
  "Given a sequence ( a b c ), returns all tails:  ( a b c ) ( b c ) ( c )"
  (when (seq v) 
    (lazy-cat (list v) (tails (rest v)))))

(defn pair* [v]
  "Match the first item in the list with all others in pairs."
  (when (> (count v) 1)
    (for [y v] [(first v) y])))

(defn all-pairs [v]
  (apply concat (map pair* (tails v))))


How about this?

(defn all-pairs [coll]
(when coll
  (concat (map vector (repeat (first coll)) (rest coll))
          (all-pairs (next coll)))))

Or, if you seek a lazy seq:

(defn all-pairs [coll]
(lazy-seq
  (when coll
    (concat (map vector (repeat (first coll)) (rest coll))
            (all-pairs (next coll))))))


What about this?

(defn seq->pairs 
  [s]
  (loop [res [] s s]
     (let [[head next] (split-at 2 s)
           res (conj res head)]
        (if (empty? next) res (recur res next)))))


Just another possible solution:

(defn all-pairs
        [c]
        (mapcat #(drop % %2)
                 (range 1 (count c))
                 (partition (count c) (for [a c b c] [a b]))))


(all-pairs '(a b c d)) => ([a b] [a c] [a d] [b c] [b d] [c d]))
(all-pairs [5 4 3 2 1]) => ([5 4] [5 3] [5 2] [5 1] [4 3] [4 2] [4 1] [3 2] [3 1] [2 1])
(all-pairs "pairs") => ([\p \a] [\p \i] [\p \r] [\p \s] [\a \i] [\a \r] [\a \s] [\i \r] [\i \s] [\r \s])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号