fix: conj 0-arg, conj onto nil (builds list), conj map into map
- (conj) -> []
- (conj nil x ...) builds a list (prepends): (conj nil 1 2) -> (2 1)
- conj a map value into a map/phm merges its entries ((conj {:a 0} {:b 1}) ->
{:a 0 :b 1}); a [k v] vector/pair still adds one entry.
conj.cljc 14/6/5 -> 21/3/1. clojure-test-suite pass 3681->3691, errors 105->98.
spec: seq/conj-edge-cases (8). jpm test green.
This commit is contained in:
parent
a4d9d5f70b
commit
66c8c1157b
3 changed files with 33 additions and 13 deletions
|
|
@ -283,7 +283,16 @@
|
|||
# Collections
|
||||
# ============================================================
|
||||
|
||||
(defn core-conj [coll & xs]
|
||||
# Is x a map value (for conj/merge semantics: conj-ing a map merges its entries)?
|
||||
(defn- map-value? [x]
|
||||
(or (phm? x) (and (struct? x) (nil? (get x :jolt/type)))))
|
||||
|
||||
(defn core-conj [& args]
|
||||
(if (= 0 (length args)) (make-vec @[]) # (conj) -> []
|
||||
(let [coll (first args) xs (tuple/slice args 1)]
|
||||
(if (nil? coll)
|
||||
# conj onto nil builds a list (prepends): (conj nil 1 2) -> (2 1)
|
||||
(do (var result nil) (each x xs (set result (pl-cons x result))) result)
|
||||
(if (pvec? coll)
|
||||
(do (var result coll) (each x xs (set result (pv-conj result x))) result)
|
||||
(if (plist? coll)
|
||||
|
|
@ -303,20 +312,20 @@
|
|||
(if (phm? coll)
|
||||
(do
|
||||
(var result coll)
|
||||
(var i 0)
|
||||
(while (< i (length xs))
|
||||
(let [pair (xs i)]
|
||||
(set result (phm-assoc result (vnth pair 0) (vnth pair 1))))
|
||||
(++ i))
|
||||
(each x xs
|
||||
(if (map-value? x)
|
||||
# conj a map -> merge its entries
|
||||
(each k (if (phm? x) (keys (phm-to-struct x)) (keys x))
|
||||
(set result (phm-assoc result k (if (phm? x) (phm-get x k) (in x k)))))
|
||||
(set result (phm-assoc result (vnth x 0) (vnth x 1)))))
|
||||
result)
|
||||
(do
|
||||
(var result coll)
|
||||
(var i 0)
|
||||
(while (< i (length xs))
|
||||
(let [pair (xs i)]
|
||||
(set result (merge result {(vnth pair 0) (vnth pair 1)})))
|
||||
(++ i))
|
||||
result))))))))
|
||||
(each x xs
|
||||
(if (map-value? x)
|
||||
(set result (merge result (if (phm? x) (phm-to-struct x) x)))
|
||||
(set result (merge result {(vnth x 0) (vnth x 1)}))))
|
||||
result)))))))))))
|
||||
|
||||
(defn core-assoc [m & kvs]
|
||||
(cond
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue