Skip to content

CLJS-3324: hash-map behavior differs from Clojure #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8052,7 +8052,9 @@ reduces them without incurring seq initialization"
(let [len (alength ks)]
(loop [i 0 ^not-native out (transient (.-EMPTY PersistentHashMap))]
(if (< i len)
(recur (inc i) (-assoc! out (aget ks i) (aget vs i)))
(if (<= (alength vs) i)
(throw (js/Error. (str "No value supplied for key: " (aget ks i))))
(recur (inc i) (-assoc! out (aget ks i) (aget vs i))))
(persistent! out))))))

(set! (.-createWithCheck PersistentHashMap)
Expand Down Expand Up @@ -8926,7 +8928,10 @@ reduces them without incurring seq initialization"
[& keyvals]
(loop [in (seq keyvals), out (transient (.-EMPTY PersistentHashMap))]
(if in
(recur (nnext in) (assoc! out (first in) (second in)))
(let [in' (next in)]
(if (nil? in')
(throw (js/Error. (str "No value supplied for key: " (first in))))
(recur (next in') (assoc! out (first in) (first in')) )))
(persistent! out))))

(defn array-map
Expand Down
7 changes: 5 additions & 2 deletions src/main/clojure/cljs/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2623,9 +2623,12 @@
(core/defmacro hash-map
([] `(.-EMPTY cljs.core/PersistentHashMap))
([& kvs]
(core/let [pairs (partition 2 kvs)
(core/let [pairs (map
(core/fn [pair]
(remove #{::missing} pair))
(partition 2 2 (repeat ::missing) kvs))
ks (map first pairs)
vs (map second pairs)]
vs (map second (take-while #(= 2 (count %)) pairs))]
(vary-meta
`(.fromArrays cljs.core/PersistentHashMap (array ~@ks) (array ~@vs))
assoc :tag 'cljs.core/PersistentHashMap))))
Expand Down
7 changes: 7 additions & 0 deletions src/test/cljs/cljs/collections_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,13 @@
(testing "persistent vector invoke matches clojure"
(is (thrown-with-msg? js/Error #"Key must be integer" ([1 2] nil)))))

(deftest test-cljs-3324
(testing "hash-map behavior with missing values matches clojure"
(is (thrown-with-msg? js/Error #"No value supplied for key: :a"
(hash-map :a)))
(is (thrown-with-msg? js/Error #"No value supplied for key: :a"
(apply hash-map [:a])))))

(comment

(run-tests)
Expand Down