我能想到的最好的方法是:
(defn dups [seq]
(map (fn [[id freq]] id)
(filter (fn [[id freq]] (> freq 1))
(frequencies seq))))
有更简洁的方式吗?
我能想到的最好的方法是:
(defn dups [seq]
(map (fn [[id freq]] id)
(filter (fn [[id freq]] (> freq 1))
(frequencies seq))))
有更简洁的方式吗?
(defn dups [seq]
(for [[id freq] (frequencies seq) ;; get the frequencies, destructure
:when (> freq 1)] ;; this is the filter condition
id)) ;; just need the id, not the frequency
(map key (remove (comp #{1} val)
(frequencies seq)))
(defn dups-with-function
[seq f]
(->> seq
(group-by f)
; filter out map entries where its value has only 1 item
(remove #(= 1 (count (val %))))))
(let [seq [{:attribute :one
:other-things :bob}
{:attribute :one
:other-things :smith}
{:attribute :two
:other-things :blah}]]
(dups-with-function seq :attribute))
输出:
([:one
[{:attribute :one, :other-things :bob}
{:attribute :one, :other-things :smith}]])
(dups-with-function my-list #(.getFirstName %))
以下是一行代码,可以实现最小的过滤和频率处理:
(filter #(< 1 ((frequencies col) %)) col)
然而它在大量数据上表现不佳。您需要通过以下方式帮助编译器:
(let [ freqs (frequencies col) ]
(filter #(< 1 (freqs %)) col))
some
是这种情况下完美的函数。
(defn dups [coll]
(some (fn [[k v]] (when (< 1 v) k))
(frequencies coll)))