I've used this approach before. It was simple and worked well.  Note that assoc 
can take multiple keys and vals so you can simplify it to (assoc m k v v k) 
instead of nesting the assoc calls.  If there's any chance of a collision, you 
could be defensive and test for (contains? m k) or (contains? m v) and take 
appropriate action (maybe throw).  It might be a good thing to put into a 
pre-condition.


On May 25, 2011, at 11:12 PM, Ken Wesson wrote:

> If the token and id sets are disjoint (no single object can ever
> appear as each -- only one of those at most) then you can use a single
> map with a function put defined as:
> 
> (defn put [m k v]
>  (assoc (assoc m k v) v k))
> 
> which adds mappings in both directions. Then just use (m k) or (m v)
> to perform lookups in either direction. The overhead may be less than
> with two separate hashmaps, and the code is certainly simpler.

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