Clojure Spectre:如何找到具有特定值的映射键?

Clojure Specter: how to find map keys that have specific value?

例如在地图中:

{"test-1" 23,
 "test-2" 456,
 "test-3" 23}

如何查找具有值 23 的键?

我认为您不需要幽灵来执行此操作,只需按值过滤即可。即:

(->> {:key-1 10
      :key-2 20
      :key-3 10}
      (filter (fn [[k v]] (= v 10)))
      (map first))
==>  [:key-1 :key-3]

使用 Spectre 的解决方案是:

(keys (specter/setval [specter/MAP-VALS #(not= 10 %)]
                       specter/NONE
                       {:key-1 10
                        :key-2 20
                        :key-3 10}))

如果你想用Spectre找东西,最好用specter/select.

(use 'com.rpl.specter)    

(select [ALL #(= (second %) 23) FIRST]
  {"test-1" 23,
   "test-2" 456,
   "test-3" 23})