Search code examples
clojurezipmap

zipmap with multi-value keys


The following:

(zipmap '(:a :b :c :c) '(1  2  3  4))

evals to: {:c 4, :b 2, :a 1}

I would like to get:

{:c '(3 4) :b '(2) :a '(1)}

instead.

How should I define my own zipmap that takes two lists and returns a map with multiple values for keys?


Solution

  • This will do

    (defn zippy [l1 l2]
      (apply merge-with concat (map (fn [a b]{a (list b)}) l1 l2)))
            ;;; ⇒ #'user/zippy
    
    (zippy '(:a :b :c :c) '(1  2  3  4))
            ;;; ⇒ {:c (3 4), :b (2), :a (1)}