我是Clojure的新手,我正在尝试使用update-in和匿名函数更新地图
(def items {:my-item {:item-count 10}})
(update-in items [:my-item :item-count]
(fn [%] (- (get-in items [:my-item :item-count]) 3)))预期的结果是项目计数现在应该是7,我的代码可以工作,但我想知道我是否可以在不调用get-in方法的情况下做到这一点。
我尝试过的另一种方法如下:
(update-in items [:my-item :item-count]
(dec (fn [%] 3)))这给了我
cannot be cast to java.lang.Number发布于 2019-08-15 00:35:18
你的匿名函数应该接受它想要修改的东西,并使用它,而不是做get-in
(update-in items [:my-item :item-count]
(fn [item-count] (- item-count 3)))发布于 2019-08-14 05:26:30
(update-in items [:my-item :item-count] - 3)发布于 2019-08-14 05:59:49
(def items {:my-item {:item-count 10}})
(update-in items [:my-item :item-count] #(- % 3))https://stackoverflow.com/questions/57485493
复制相似问题