<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged sorted-map</title>
<link>https://ask.clojure.org/index.php/tag/sorted-map</link>
<description></description>
<item>
<title>`update-vals` not preserving sorted maps?</title>
<link>https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps</link>
<description>&lt;p&gt;Hello! We recently found that when applying &lt;code&gt;update-vals&lt;/code&gt; to a sorted map, it's converted back into an unsorted array/hash map due to the collection not being transientable. Example given below. As a note, this differs from &lt;code&gt;medley/map-vals&lt;/code&gt; which preserves sorting and which we were using for this functionality previously to 1.11. &lt;/p&gt;
&lt;p&gt;Is this intended behavior? If so, I'd be curious about the intent behind it :) Thanks!&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;user&amp;gt; (def m (sorted-map :a 0 :b 0))
;; =&amp;gt; #'user/m
user&amp;gt; (type m)
;; =&amp;gt; clojure.lang.PersistentTreeMap
user&amp;gt; (type (update-vals m inc))
;; =&amp;gt; clojure.lang.PersistentArrayMap
user&amp;gt; (type (medley.core/map-vals inc m))
;; =&amp;gt; clojure.lang.PersistentTreeMap
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Collections</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12073/update-vals-not-preserving-sorted-maps</guid>
<pubDate>Wed, 27 Jul 2022 08:35:05 +0000</pubDate>
</item>
<item>
<title>Bug when using nested sorted maps</title>
<link>https://ask.clojure.org/index.php/10377/bug-when-using-nested-sorted-maps</link>
<description>&lt;p&gt;After discussing it on Slack, the following definitely feels like a bug. At the very least, it throws weirdly in CLJS while it is perfectly (and intuitively) fine in Clojure JVM.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(def tree
     (sorted-map 3
                 (sorted-map 1
                             {:a 'leaf-A})))
(def subtree
     (sorted-map 1
                 (sorted-map 100
                             {:b 'leaf-B})))
;; Fine, of course, but now...
(assoc tree
       3
       subtree)
;; Throws:   Error: Cannot compare :a to 100
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Andy Fingerhut pointed to a difference in implementation. Clojure compares by reference and CLJS by value, forcing a deep comparison. Even then, I find the error cryptic and argue that logically speaking, Clojure makes sense.&lt;/p&gt;
&lt;p&gt;Citing him, Clojure implementation:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentTreeMap.java#L132&quot;&gt;https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentTreeMap.java#L132&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And CLJS implementation: &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L8857&quot;&gt;https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L8857&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Is there something we didn't see?&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10377/bug-when-using-nested-sorted-maps</guid>
<pubDate>Fri, 26 Mar 2021 08:47:17 +0000</pubDate>
</item>
</channel>
</rss>