Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zipmap with multi-value keys

Tags:

clojure

zipmap

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?

like image 850
Marcus Junius Brutus Avatar asked Jun 29 '26 16:06

Marcus Junius Brutus


1 Answers

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)}
like image 137
Joe Lehmann Avatar answered Jul 04 '26 02:07

Joe Lehmann