设计是有意为之,这是为了性能。排序的映射主要用于调试。
如果您想让您的排序映射正常工作,请使用此答案底部的代码。
```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)))
```