4clojure #61 Map Construction

4clojure #61 Map Construction

ベクタkeysvaluesをとり、それらからマップを生成する関数を作る。

zipmapは使用禁止。

(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
(= (__ [1 2 3 4] ["one" "two" "three"]) {1 "one", 2 "two", 3 "three"})
(= (__ [:foo :bar] ["foo" "bar" "baz"]) {:foo "foo", :bar "bar"})
(= ((fn [keys vals] (apply hash-map (interleave keys vals)))
    [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
参考zipmapの定義
(defn zipmap
  "Returns a map with the keys mapped to the corresponding vals."
  {:added "1.0"
   :static true}
  [keys vals]
    (loop [map {}
           ks (seq keys)
           vs (seq vals)]
      (if (and ks vs)
        (recur (assoc map (first ks) (first vs))
               (next ks)
               (next vs))
        map)))
参考 マップを作りたい
(array-map :a 1 :b "a" :d 'foo :c :bar)
; => {:a 1, :b "a", :d foo, :c :bar}
(hash-map :a 1 :b "a" :d 'foo :c :bar)
; => {:a 1, :c :bar, :b "a", :d foo}
(sorted-map :a 1 :b "a" :d 'foo :c :bar)
; => {:a 1, :b "a", :c :bar, :d foo}