On 7 Oct 2008, at 21:00, Brian Doyle wrote:

> [...] I've created a defstruct for a place type object like:
>
> (defstruct place :id :name :street :city :state :zip)
>
> I want to write a function that would take a list of places and remove
> the duplicates.   Duplicates are defined by places having the  
> same :id.

How about something like this (based on "distinct" in boot.clj):

(defn hashed-distinct
   "Returns a lazy sequence of the elements of coll with duplicates  
removed after elements first 'hashed' by hashfn"
   [coll hashfn]
     (let [step (fn step [[f & r :as xs] seen]
                    (when xs
                      (if (seen (hashfn f))
                       (recur r seen)
                       (lazy-cons f (step r (conj seen (hashfn f)))))))]
       (step (seq coll) #{})))

(defstruct place :id :name :street :city :state :zip)

(defn distinct-places [places]
   (hashed-distinct places #(:id %1)))

So the "hashing" or "distinction extracting" function is passed into  
the duplicate removing function.  It seems odd to me to define an  
equality such as this as being the property of the object.  If the  
object has externally visible attributes that differ then to define =  
to say that it is the same can only be a source of confusion surely.

stephen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to