function/macro | since v0.0-1211 | clojure.core/array-map | Edit |
(array-map & keyvals)
Returns a new array map (a map implemented with arrays) with the supplied mappings.
keyvals
must be an even number of forms.
(array-map :a 10)
;;=> {:a 10}
(array-map :a 10 :b 20)
;;=> {:a 10 :b 20}
keyval => key val Returns a new array map with supplied mappings.
(defn array-map
[& keyvals]
(let [arr (if (and (instance? IndexedSeq keyvals) (zero? (.-i keyvals)))
(.-arr keyvals)
(into-array keyvals))]
(if (odd? (alength arr))
(throw (js/Error. (str "No value supplied for key: " (last arr))))
(.createAsIfByAssoc PersistentArrayMap arr))))
(core/defmacro array-map
([] '(.-EMPTY cljs.core/PersistentArrayMap))
([& kvs]
(core/let [keys (map first (partition 2 kvs))]
(if (core/and (every? #(= (:op (cljs.analyzer/unwrap-quote %)) :const)
(map #(cljs.analyzer/no-warn (cljs.analyzer/analyze &env %)) keys))
(= (count (into #{} keys)) (count keys)))
`(cljs.core/PersistentArrayMap. nil ~(clojure.core// (count kvs) 2) (array ~@kvs) nil)
`(.createAsIfByAssoc cljs.core/PersistentArrayMap (array ~@kvs))))))