JanetDocsPlaygroundI'm feeling luckyGitHub sign in

update-in



    function
    boot.janet on line 1471, column 1

    (update-in ds ks f & args)

    Update a value in a nested data structure `ds`. Looks into `ds` via 
    a sequence of keys, and replaces the value found there with `f` 
    applied to that value. Missing data structures will be replaced 
    with tables. Returns the modified, original data structure.


4 examplesSign in to add an example
Loading...
(def ds @[@{:a 1 :b 2}
          @{:a 8 :b 9}])

(defn thrice [x] (* x 3))

(update-in ds [0 :b] thrice)

# `ds` is now
@[@{:a 1 :b 6}
  @{:a 8 :b 9}]
uvtcPlayground
(defn ddup [ds ks val]
  (update-in ds ks
    (fn [x]
      (if (= nil x)
        @[val]
        (array/push x val)))))

(var a @{})
(ddup a [:a] 1)
(ddup a [:a] 2)
(ddup a [:a] 3)
# @{:a @[1 2 3]}

sbjaverPlayground
(update-in @{:a @{:b 1}} [:a :b] (fn [x] (+ 1 x)))
# @{:a @{:b 2}}
sbjaverPlayground
(update-in @{:a 1} [:a] (fn [x] (+ 1 x)))
# @{:a 2}
sbjaverPlayground