这是有意为之,是为了性能。排序映射主要是用于调试。
如果你想让你的排序映射工作,请使用此答案底部的代码。
```clojure
(defn update-vals
"m f => {k (f v) ...}
Given a map m and a function f of 1-argument, returns a new map where the keys of m
are mapped to result of applying f to the corresponding values of m."
{:added "1.11"}
[m f]
(with-meta
(persistent!
(reduce-kv (fn [acc k v] (assoc! acc k (f v)))
(if (instance? clojure.lang.IEditableCollection m)
(transient m)
(transient (empty m)))
m))
(meta m)))
(update-vals
(sorted-map
:d :e
:b :c
:h :a
:a :g
)
str)
```
错误
```clojure
1. Unhandled java.lang.ClassCastException
class clojure.lang.PersistentTreeMap cannot be cast to class clojure.lang.IEditableCollection
(clojure.lang.PersistentTreeMap and clojure.lang.IEditableCollection are in unnamed module of loader 'app')
```
与排序映射一起工作的代码,但速度较慢
```clojure
(defn update-vals
"m f => {k (f v) ...}
Given a map m and a function f of 1-argument, returns a new map where the keys of m
are mapped to result of applying f to the corresponding values of m."
{:added "1.11"}
[m f]
(with-meta
(reduce-kv (fn [acc k v] (assoc acc k (f v)))
(empty m)
m)
(meta m)))
```