This is the easiest way to swap the keys with the values in a hash-map.
(defn flip-map [m]
	(zipmap (vals m) (keys m)))
#'user/flip-map
user> (def m1 (apply hash-map (range 8)))
#'user/m1
user> m1
{0 1, 2 3, 4 5, 6 7}
user> (def m2 (flip-map m1))
#'user/m2
user> m2
{7 6, 5 4, 3 2, 1 0}
 
11.06.2013, 14:30, "Michael-Keith Bernard" <mkbernard....@gmail.com>:
An alternative implementation that shuffles all printable characters. This version returns the encryption map and generalizes the encrypt/decrypt functionality.
 
https://gist.github.com/SegFaultAX/5754209

On Tuesday, August 12, 2008 10:25:00 PM UTC-7, Joubert Nel wrote:
Hello,

I have a hash map, and want to derive another map from it but with the
key/value pairs swapped.

To do this, I wrote:

(def *reverseMap* (loop [result (hash-map)
                         keys (keys *myMap*)]
                    (if (== (count keys) 0)
                      result
                      (recur (merge result (hash-map (val (find *myMap* (first
keys)))
                                                     (first keys)))
                             (rest keys)))))


It works fine, but I suspect there is a better way.
Any suggestions?

Joubert

 

--
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply via email to