2011-11-08 14 views
9

में डुप्लिकेट रिटर्निंग सबसे अच्छा मैं के साथ आ सकता था:एक दृश्य

(defn dups [seq] 
    (map (fn [[id freq]] id) 
     (filter (fn [[id freq]] (> freq 1)) 
       (frequencies seq)))) 

वहाँ एक और अधिक संक्षिप्त तरीका है?

+0

मैं अपने समाधान चाहते हैं, लेकिन बस (एफ एन [[आईडी freq]] आईडी) मुख्य कार्य के साथ की जगह लेंगे। –

उत्तर

16

एक सूची समझ का उपयोग करें: आप सूची में बंद आधारित डुप्लीकेट ढूंढना कुछ आइटमों की संपत्ति (चाहते हैं, तो

(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 
11
(map key (remove (comp #{1} val) 
       (frequencies seq))) 
+0

क्या आप समझा सकते हैं कि (comp # {1} val) क्या कर रहा है? Thxs। –

+0

(comp # {1} val) मूल रूप से मतलब है (fn [x] (# {1} (val x)) - मूल रूप से, यह परीक्षण करता है कि तर्क का मूल्य 1 है (यदि यह संख्या वाले सेट में निहित है 1)। वैल्यू आवृत्ति जोड़ी में गिनती है। –

4

यानी यह या नक्शों की सूची रिकॉर्ड की एक सूची है/जावा वस्तुओं)

(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}]]) 
,210

आप जावा वस्तुओं की एक सूची है और आप डुप्लिकेट पहला नाम के साथ सभी लोगों को खोजने के लिए चाहते हैं: oneliner कि काम करता है

(dups-with-function my-list #(.getFirstName %)) 
0

मिनिमल फिल्टर और आवृत्तियों:

(filter #(< 1 ((frequencies col) %)) col) 

हालांकि यह प्रदर्शन बड़े डेटा पर खराब। आप कह कर संकलक मदद करने के लिए होगा:

(let [ freqs (frequencies col) ] 
    (filter #(< 1 (freqs %)) col))