On Oct 3, 9:32 am, 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'm sure you'll get better answers later but this is the pseudo code
for what I would do.

1. Separate  your objects into clojure sets. This makes it quick and
easy to list the objects in a given set.
2. To find out which set an object is in, do a get on each set until a
non nil return value is seen using (some #(% obj) [my-sets])

>
> 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).
>

3. Only the mapping, object => set, is needed and, as you say, this is
an implementation detail that should be hidden. So memoize step 2 for
performance. You get a global map but it doesn't clutter your code.

Saul

-- 
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

Reply via email to