在Clojure中迭代所有对集合的惯用方法

时间:2010-10-29 16:22:02

标签: clojure sequence

给定一个集合,我想迭代集合中的所有对。实施例

(all-pairs seq)

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

这是我的想法

(defn all-pairs [coll]
  (for [ [idx elmt] (indexed coll)
         other-elmt (subvec coll (inc idx))]
     (vector elmt other-elm)))

但它并不觉得惯用

10 个答案:

答案 0 :(得分:17)

怎么样:

(use 'clojure.contrib.combinatorics)
(vec (map vec (combinations '(a b c d) 2)))

答案 1 :(得分:6)

懒惰,而且相对较快。

(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])

答案 2 :(得分:6)

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

答案 3 :(得分:4)

我可以建议:

(defn all-pairs [sq] (for [i sq j sq] [i j]))
编辑:显然我误解了这个问题;因为你只想要不同的非重复对,所以如果在你调用这个函数的任何域上存在自然排序,我们仍然可以使用这种方法。

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

编辑2

此外:

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

答案 4 :(得分:1)

如果你想用&#34;学术风格编写自己的combinations功能,&#34;你可以尝试

(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)))))

然后将其应用于您的问题,如下所示

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

答案 5 :(得分:0)

一个简单的递归版本,可以做你想要的:

(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)))))

答案 6 :(得分:0)

不是最快的解决方案,但是:

; 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))))

答案 7 :(得分:0)

这个怎么样?

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

或者,如果你寻求懒惰的seq:

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

答案 8 :(得分:0)

这个怎么样?

(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)))))

答案 9 :(得分:0)

另一种可能的解决方案:

(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])