是否有一种直接的方式来选择一个子映射,其中包含嵌套映射中每个映射值的转换子映射,使用specter?
例如:cr是一个嵌套映射,
{:marks {:these :values :are :omitted},
:scores {:to :simplify :the :example},
:results {1 {:total 8, :sums {:p1 5, :p2a 3}, :check true},
2 {:total 8, :sums {:p1 9, :p2b -1}, :check false}}}从这里我想提取一个映射(int ->布尔值),包含:results中的每个键,与:check键相关联的值,
在本例中,{1 true 2 false}
我可以分两步
(:results (spc/transform [:results spc/MAP-VALS] :check cr))或者,在一般情况下,所需的子映射不在顶层。
(spc/select-one [...... :results] (spc/transform .... cr))没有幽灵,这种转变可以用同样的方式来表达(可以说也同样清晰)。
(mapmap #(:check %2) (:results cr))地图在哪里
(defn mapmap
"creates a map, by mapping f over the values in m
f is a function with two arguments, and is passed
the key and the value "
[f m]
(reduce-kv #(assoc %1 %2 (f %2 %3) ) {} m)
)我觉得我错过了什么东西,因为我不能用幽灵来表达它作为一个单一的导航。这个查询能用specter在单个select或transform中表示吗?
Recursive map query using specter似乎是相关的,但我不太了解递归路径是如何工作的,也不太清楚如何将它们用于转换。
发布于 2018-04-20 16:27:56
缺失的部分是transformed导航器:
(transformed path update-fn)
Navigates to a view of the current value by transforming it with the
specified path and update-fn.可以在所需的一行中使用。
(spc/select-one [:results (spc/transformed [spc/MAP-VALS] :check)] cr)
==> {1 true, 2 false}https://stackoverflow.com/questions/49940602
复制相似问题