Skip to content

Commit

Permalink
CLJS-459: fix reduce-kv node visit order for sorted maps
Browse files Browse the repository at this point in the history
Includes a basic test.
  • Loading branch information
michalmarczyk authored and swannodette committed Jan 27, 2013
1 parent e8d2022 commit 306ff22
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/cljs/cljs/core.cljs
Expand Up @@ -5030,12 +5030,12 @@ reduces them without incurring seq initialization"
(throw (js/Error. "red-black tree invariant violation"))))

(defn- tree-map-kv-reduce [node f init]
(let [init (f init (.-key node) (.-val node))]
(let [init (if-not (nil? (.-left node))
(tree-map-kv-reduce (.-left node) f init)
init)]
(if (reduced? init)
@init
(let [init (if-not (nil? (.-left node))
(tree-map-kv-reduce (.-left node) f init)
init)]
(let [init (f init (.-key node) (.-val node))]
(if (reduced? init)
@init
(let [init (if-not (nil? (.-right node))
Expand Down
4 changes: 4 additions & 0 deletions test/cljs/cljs/core_test.cljs
Expand Up @@ -1773,6 +1773,10 @@
(let [x (fn [] "overwritten")]
(assert (= "original" (y)))))

;; CLJS-459: reduce-kv visit order
(assert (= (reduce-kv conj [] (sorted-map :foo 1 :bar 2))
[:bar 2 :foo 1]))

;; Test builtin implementations of IKVReduce
(letfn [(kvr-test [data expect]
(assert (= :reduced (reduce-kv (fn [_ _ _] (reduced :reduced))
Expand Down

0 comments on commit 306ff22

Please sign in to comment.