On Sep 20, 12:08 pm, Alan <a...@malloys.org> wrote:
> A few days ago I was thinking about how different it would be to write
> flip-map* in Java vs Clojure. A very simple, small program, but easy
> to see how Clojure can be more expressive.
>
> public static Map<V,K> flipMap(Map<K,V> map)
> {
>     Map<V,K> result = new HashMap<V,K>(map.size());
>     for (K key : map.keys()) {
>         result.put(map.get(key), key);
>     }
>
>     return result;
>
> }
>
> (def flip-map #(apply zipmap ((juxt vals keys) %)))
>
> * Given a hash map, reverse its keys and values such that every K=>V
> pair in the input map becomes V=>K in the output. Naturally the values
> must be unique for this to work, so call that a precondition.
>
> On Sep 17, 2:47 pm, anderson_leite <anderson...@gmail.com> wrote:

If I was going to write flip-map it might be a tad longer, but lazy
and a bit clearer (IMO), with:

    (defn flip-map [m] (into {} (map (juxt second first) m)))

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