On Mar 12, 2009, at 3:34 PM, Kyle Schaffrick wrote:
> > On Thu, 12 Mar 2009 12:45:00 -0700 (PDT) > Jason Wolfe <jawo...@berkeley.edu> wrote: >>> Also, union/difference/intersection/symmetric-diff are binary. Would >>> there be any interest in a patch to make them n-ary? >>> >> >> Union, difference, and intersection are all variadic as of a month or >> so ago. Are you on the latest SVN? >> >> -Jason > > Oops, so they are. I am actually on SVN but referred to (apparently) > out-of-date docs. That, or Rich has joined the circle of > time-machine-owning dynamic-language-designing BDFLs. > > In that case, here's my two stabs at n-ary set symmetric difference: > > (defn symmetric-diff [& sets] > (let [all-members (apply union sets) > nr-memberships > (fn [m] (apply + (for [s sets :when (contains? s m)] 1))) > in-sym-diff (fn [m] (odd? (nr-memberships m)))] > (set (filter in-sym-diff (seq all-members))))) > > (defn symmetric-diff > ([s1] s1) > ([s1 s2] > (difference (union s1 s2) > (intersection s1 s2))) > ([s1 s2 & sets] > (reduce symmetric-diff s1 (conj sets s2)))) > > -Kyle IIRC the docs all correspond to the last release, which was a couple months ago. This means that by now they are quite out of date, but at least they're stable for people who shy away from the bleeding edge. I suppose this might be good in clojure.contrib.set, if any of the primary contributors want to OK it. Have you done any timing tests? The first version could be improved by adding a "distinct" on all- members before the filter. My guess is that the second version would still be faster, but I'm not sure. Also, (union (difference s1 s2) (difference s2 s1)) may be faster than (difference (union s1 s2) (intersection s1 s2)), but again I'm not sure. -Jason --~--~---------~--~----~------------~-------~--~----~ 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 clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---