clojure/core update-in

点我查看原文及更多示例

(ns $clojureDoc.core.update-in)
;; 使用方法:(update-in m ks f & args)
;; 'Updates' a value in a nested associative structure, where ks is a
;; sequence of keys and f is a function that will take the old value
;; and any supplied args and return the new value, and returns a new
;; nested structure.  If any levels do not exist, hash-maps will be
;; created.

;; 嵌套
(println (update-in {:a {:b {:c 3}} :b 2 :c 3} [:a :b :c] + 4 5))
;; => {:a {:b {:c 12}}, :b 2, :c 3}
(println (update-in {:a 1 :b {:b {:c 3}} :c 3} [:b :b :c] + 4 5))
;; => {:a 1, :b {:b {:c 12}}, :c 3}
;; 从上面例子看出
;; ks是一层一层的key,这个指定获取哪个key的value
;; 拿到value,和最后的可变参数一起参与函数f计算后,将value替换

;; 这个例子看懂了吗?
(println (update-in [{1 'a 2 'b 3 'c}] [1 2 3] str 4 5 6))
;; => [{1 :A, 2 :B, 3 :C} {2 {3 456}}]

;; 没看懂看这里
;; 由contains?函数介绍知道,对于vector这样的序列(list排除哈),索引即为键!
;; 那么ks现在是[0 :A],所以获取vector [{:A 4 :B 5 :C 6}]的第0项,{:A 4 :B 5 :C 6},然后获取key :A
(println (update-in [{:A 4 :B 5 :C 6}] [0 :A] + 4 5 6))
;; => [{:A 19, :B 5, :C 6}]

;; 现在回来看没懂的例子
;; 如果使用(nth [0 1 2] 3)将会报错
;; 但使用contains?会出现false,这很正常吧,那理解上面没懂的例子需要这个概念
(println (contains? [0 1 2] 3))
;; => false
;; 因为1“不存在”,我们可以认为是key-value的键值对为:1 nil
;; 那1的value运算后就是 (str nil 4 5 6) => 456,键key就是1
;; 放回去,原vector就有了两个元素 0 1
(println (update-in [{1 'a 2 'b 3 'c}] [1] str 4 5 6))
;; => [{1 a, 2 b, 3 c} 456]
;; 1为nil,那么nil中的key没有2,那么就创建呀
(println (update-in [{1 'a 2 'b 3 'c}] [1 2] str 4 5 6))
;; => [{1 a, 2 b, 3 c} {2 456}]
;; 同理喽
(println (update-in [{1 'a 2 'b 3 'c}] [1 2 3] str 4 5 6))
;; => [{1 :A, 2 :B, 3 :C} {2 {3 456}}]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangningboo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值