another way to do this, more curious, I think:
(let [items [{ :name "John" :email " john@gmail.com " } { :name "Batman" :email " batman@gmail.com " } { :name "John Doe" :email " john@gmail.com " }]] (map first (vals (group-by :email items))))
exit:
({:name "John", :email " john@gmail.com "} {:name "Batman", :email " batman@gmail.com "})
this is how it works:
(group-by :email items) makes a card whose keys are emails, and the values ββare groups of records with this email address
{" john@gmail.com " [{:name "John", :email " john@gmail.com "} {:name "John Doe", :email " john@gmail.com "}], " batman@gmail.com " [{:name "Batman", :email " batman@gmail.com "}]}
then you just need to take your shafts (groups of records) and choose the first from them.
And another way is to create a sorted email set so that it treats all records with the same email addresses as equal records:
(let [items [{ :name "John" :email " john@gmail.com " } { :name "Batman" :email " batman@gmail.com " } { :name "John Doe" :email " john@gmail.com " }]] (into (sorted-set-by
exit:
#{{:name "Batman", :email " batman@gmail.com "} {:name "John", :email " john@gmail.com "}}
they really donβt know which one is more idiomatic and has better performance. But I bet on the first.
leetwinski
source share