On Sun, 3 Oct 2010 00:32:16 -0700 (PDT) Alan <a...@malloys.org> wrote:
> I've got a collection of unique objects, and I need to partition them > into sets. That part's easy enough, but I need to have both of the > following be efficient, and preferably easy: > - Given an object, determine what set it's in > - List all the objects in a given set > > I could construct all the objects and have a single "global" map, with > mappings for both set-id=>[objects] and object=>set-id, but this seems > kinda gross and obscures what is actually meant (objects belong to > sets) with implementation (integers/keywords mapping to groups of > objects, and objects mapping to integers). That's the right idea, but it's overkill. You only need to provide indirection for one direction, not both. For the other direction, you can use the items directly. Chance are either objects or sets of them have a natural "name" you can use for the id. If the sets have the natural ids, then you should be able to write a function create-object that creates a map representing an object, including a :set key that is the id of the set it belongs to. Finally, given (source) that produces a list vectors of features for each object, you do: (def set-map (group-by :set (map create-object (source)))) Given an object, you can get the set with (set-map (:set object)). Given a set - it is a list of objects. If the objects have the natural id - that's an exercise for the reader. <mike -- Mike Meyer <m...@mired.org> http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information. O< ascii ribbon campaign - stop html mail - www.asciiribbon.org -- 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 Note that posts from new members are moderated - please be patient with your first post. 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