Search code examples
clojurezipmap

What does Clojure's zip-map do?


I am new at Clojure and I needed help with this function. If you could please tell me what this function does and how it works I would be really thankfull.

(defn zip-map
  [k v]
    (into{} (map vec (partition 2 (interleave k v)))))

Solution

  • Example of usage:

    (zip-map [:a :b :c] [1 2 3]) ;=> {:a 1, :b 2, :c 3}    
    

    And from the inside out:

    (interleave [:a :b :c] [1 2 3]) ;=> (:a 1 :b 2 :c 3)
    (partition 2 '(:a 1 :b 2 :c 3)) ;=> ((:a 1) (:b 2) (:c 3))
    (map vec '((:a 1) (:b 2) (:c 3))) ;=> ([:a 1] [:b 2] [:c 3])
    (into {} '([:a 1] [:b 2] [:c 3])) ;=> {:a 1, :b 2, :c 3}