Permalink
Browse files

CLJS-784: make conj on maps behave as it does in Clojure

conj on maps now supports two types of items to be conj'd:

1. vectors interpreted as map entries,

2. non-vector seqables of vectors of map entries,

throwing when passed a seqable that contains non-vector items.
  • Loading branch information...
1 parent 4381c04 commit 3d4405b9b22d36e2e686a084c54ae3f6e5a6208a @michalmarczyk michalmarczyk committed with David Nolen Apr 22, 2014
Showing with 35 additions and 5 deletions.
  1. +24 −5 src/cljs/cljs/core.cljs
  2. +11 −0 test/cljs/cljs/core_test.cljs
View
29 src/cljs/cljs/core.cljs
@@ -4302,7 +4302,14 @@ reduces them without incurring seq initialization"
(-conj [coll entry]
(if (vector? entry)
(-assoc coll (-nth entry 0) (-nth entry 1))
- (reduce -conj coll entry)))
+ (loop [ret coll es (seq entry)]
+ (if (nil? es)
+ ret
+ (let [e (first es)]
+ (if (vector? e)
+ (recur (-assoc ret (-nth e 0) (-nth e 1))
+ (-next es))
+ (throw (js/Error. "conj on a map takes map entries or seqables of map entries"))))))))
IEmptyableCollection
(-empty [coll] (-with-meta cljs.core.PersistentArrayMap.EMPTY meta))
@@ -5128,7 +5135,14 @@ reduces them without incurring seq initialization"
(-conj [coll entry]
(if (vector? entry)
(-assoc coll (-nth entry 0) (-nth entry 1))
- (reduce -conj coll entry)))
+ (loop [ret coll es (seq entry)]
+ (if (nil? es)
+ ret
+ (let [e (first es)]
+ (if (vector? e)
+ (recur (-assoc ret (-nth e 0) (-nth e 1))
+ (-next es))
+ (throw (js/Error. "conj on a map takes map entries or seqables of map entries"))))))))
IEmptyableCollection
(-empty [coll] (-with-meta cljs.core.PersistentHashMap.EMPTY meta))
@@ -5866,9 +5880,14 @@ reduces them without incurring seq initialization"
(-conj [coll entry]
(if (vector? entry)
(-assoc coll (-nth entry 0) (-nth entry 1))
- (reduce -conj
- coll
- entry)))
+ (loop [ret coll es (seq entry)]
+ (if (nil? es)
+ ret
+ (let [e (first es)]
+ (if (vector? e)
+ (recur (-assoc ret (-nth e 0) (-nth e 1))
+ (-next es))
+ (throw (js/Error. "conj on a map takes map entries or seqables of map entries"))))))))
IEmptyableCollection
(-empty [coll] (with-meta cljs.core.PersistentTreeMap.EMPTY meta))
View
11 test/cljs/cljs/core_test.cljs
@@ -2169,5 +2169,16 @@
(assert (= (.toString #uuid "550e8400-e29b-41d4-a716-446655440000")
"550e8400-e29b-41d4-a716-446655440000"))
+ ;; CLJS-784
+ (doseq [m [(array-map) (hash-map) (sorted-map)]]
+ (assert (= :ok
+ (try
+ (conj m "foo")
+ (catch js/Error _
+ :ok))))
+ (assert (= {:foo 1} (conj m [:foo 1])))
+ (assert (= {:foo 1} (conj m {:foo 1})))
+ (assert (= {:foo 1} (conj m (list [:foo 1])))))
+
:ok
)

0 comments on commit 3d4405b

Please sign in to comment.