Clojure 改变原子的嵌套元素
我试图改变原子的值: 原子[{:其他站点配置2499/2500,:自有UNCI |其他配置[[30.7524999999981.0126843266559153]湿度],:其他站点UNCI 1/2500,:两个UNCI[[27.563987107797804.98485724039122 7511]湿度],:站点id 1,:其他站点数量0.86235090749618}{:其他站点配置2497/2500,:自有UNCI |其他配置[[22.976875 0.09445421388]温度],:其他站点未分配3/2500,:两个未分配[[20.920551354838697 1.06184458730517511]温度],:站点id 0,:其他站点数量0.76739906312751}0x5bf8ef86] 在本例中,我希望更改第二个地图的:tware uncl内的地图值,该地图的id为:site id 0 我试过使用:Clojure 改变原子的嵌套元素,clojure,Clojure,我试图改变原子的值: 原子[{:其他站点配置2499/2500,:自有UNCI |其他配置[[30.7524999999981.0126843266559153]湿度],:其他站点UNCI 1/2500,:两个UNCI[[27.563987107797804.98485724039122 7511]湿度],:站点id 1,:其他站点数量0.86235090749618}{:其他站点配置2497/2500,:自有UNCI |其他配置[[22.976875 0.09445421388]温度],:其他
(swap! prob-cond update-in [1 1 :both-unconf 1 1 0] 22)
但不工作。您需要通过交换!函数。
交换概率第二次更新在%[1 1:两个UNC 1 10]不断22您的数据有误,我相信:
(def data
{{:other-site-conf 2499/2500,
:own-unconf|other-conf [[30.752499999999998 1.0126843266559153 12] "Humidity"],
:other-site-unconf 1/2500,
:both-unconf [[27.563987107797804 4.984857204039122 7511] "Humidity"],
:site-id 1,
:other-site-quant 0.86235090749618}
{:other-site-conf 2497/2500,
:own-unconf|other-conf [[22.976875 0.09445843442135488 4] "Temperature"],
:other-site-unconf 3/2500,
:both-unconf [[20.920551354838697 1.0618445458730512 7511] "Temperature"],
:site-id 0,
:other-site-quant 0.7673990631272951}}
)
这里的数据是一个带有单个映射条目的映射。此MapEntry的键和值都是映射。这几乎肯定不是你想要的
我假设您想要一个包含2个贴图的向量,如下所示:
(def data
[{:other-site-conf 2499/2500,
:own-unconf|other-conf [[30.752499999999998 1.0126843266559153 12] "Humidity"],
:other-site-unconf 1/2500,
:both-unconf [[27.563987107797804 4.984857204039122 7511] "Humidity"],
:site-id 1,
:other-site-quant 0.86235090749618}
{:other-site-conf 2497/2500,
:own-unconf|other-conf [[22.976875 0.09445843442135488 4] "Temperature"],
:other-site-unconf 3/2500,
:both-unconf [[20.920551354838697 1.0618445458730512 7511] "Temperature"],
:site-id 0,
:other-site-quant 0.7673990631272951}
])
您可以按如下方式更改该值:
(let [state (atom data)
result (swap! state #(assoc-in % [1 :both-unconf 0 0] 42)) ]
结果:
[{:other-site-conf 2499/2500,
:own-unconf|other-conf
[[30.752499999999998 1.0126843266559153 12] "Humidity"],
:other-site-unconf 1/2500,
:both-unconf
[[27.563987107797804 4.984857204039122 7511] "Humidity"],
:site-id 1,
:other-site-quant 0.86235090749618}
{:other-site-conf 2497/2500,
:own-unconf|other-conf
[[22.976875 0.09445843442135488 4] "Temperature"],
:other-site-unconf 3/2500,
:both-unconf [[42 1.0618445458730512 7511] "Temperature"],
:site-id 0,
:other-site-quant 0.7673990631272951}]
这是因为顶级数据结构是一个映射。因此,当您使用[1…]关联或更新时,它将创建/更新key=1的映射条目 || ks=>[1] |assoc在{:a:b}ks 42=>{:a:b,142} || ks=>[1:均为未经确认] |assoc在{:a:b}ks 42=>{:a:b,1{:UNCO 42} || ks=>[1:均为未经确认的0] |assoc在{:a:b}ks 42=>{:a:b,1{:tware unconf{0 42}} || ks=>[1:两个未经确认的0] |assoc在{:a:b}ks 42=>{:a:b,1{:tware unconf{0{0 42}} 现在将顶层结构更改为向量,第二个元素作为贴图: [:a{:两个unc[[41 43]}] || ks=>[1] |在[:a]ks 42=>[:a 42]中的关联 || ks=>[1:均为未经确认] |在[:a]ks 42=>[:a{:均为unc 42}]中的assoc || ks=>[1:均为未经确认的0] |在[:a]ks 42=>[:a{:tware unconf[42]}]中的关联 || ks=>[1:两个未经确认的0] |在[:a]ks 42=>[:a{:均为未经确认[[42 43]]}]中的关联
我已通过以下方式获得所需输出: 重置!prob cond first@prob cond
交换%[1:两个未确认0]中的概率秒更新持续22感谢回复,但仍然存在错误。由java.lang.ClassCastException引起,无法将java.lang.Long强制转换为clojure.lang.IfSensory,更新了答案。用一个函数替换22,该函数接受要替换的值,并返回一个新值。当我将代码更改为:swap!概率第二次更新在%[1 1:两个未确认1 1 0]更新的平均值在记录get y 2中得到y 0值,仍然得到错误:由java.lang.ClassCastException引起的java.lang.Double无法强制转换为clojure.lang.ifn当我不断使用22时,我得到的是1{1{:tware uncl{1{1 22}}最后加起来。谢谢你的帮助。它帮助我达到了最终的结果。我的结构和我在问题中提到的一样。使用这个:交换!%[1:all unconf 0]42中的prob-cond assoc在结构的末尾给出:1{:all unconf{0{0 42}}}。