|
257 | 257 | _new })) |
258 | 258 |
|
259 | 259 | # @brief Update a dictionary with (key, value) pairs from a second dictionary |
260 | | -# @details The original dictionary is updated in place |
| 260 | +# @details The original dictionary is not modified |
261 | 261 | # @param _D dictionary to update |
262 | 262 | # @param _D2 second dictionary |
263 | 263 | # =begin |
264 | 264 | # (let data (dict "key" "value" "hello" "world")) |
265 | 265 | # (let new (dict "key" "new value" 5 12)) |
266 | | -# (dict:update! data new) |
267 | | -# (print data) # {key: new value, hello: world, 5: 12} |
| 266 | +# (print (dict:update data new)) # {key: new value, hello: world, 5: 12} |
| 267 | +# (print data) # {key: value, hello: world} |
268 | 268 | # =end |
269 | 269 | # @author https://github.com/SuperFola |
270 | 270 | (let update! (fun (_D _D2) { |
|
275 | 275 | (add _D _key (get _D2 _key)) |
276 | 276 | (set _i (+ 1 _i)) }) })) |
277 | 277 |
|
| 278 | +# @brief Update a dictionary with (key, value) pairs from a second dictionary |
| 279 | +# @details The original dictionary is updated in place |
| 280 | +# @param _D dictionary to update |
| 281 | +# @param _D2 second dictionary |
| 282 | +# =begin |
| 283 | +# (let data (dict "key" "value" "hello" "world")) |
| 284 | +# (let new (dict "key" "new value" 5 12)) |
| 285 | +# (dict:update! data new) |
| 286 | +# (print data) # {key: new value, hello: world, 5: 12} |
| 287 | +# =end |
| 288 | +# @author https://github.com/SuperFola |
| 289 | +(let update (fun (_D _D2) { |
| 290 | + (mut _i 0) |
| 291 | + (mut _copy (copy _D)) |
| 292 | + (let _keys (keys _D2)) |
| 293 | + (while (< _i (len _keys)) { |
| 294 | + (let _key (@ _keys _i)) |
| 295 | + (add _copy _key (get _D2 _key)) |
| 296 | + (set _i (+ 1 _i)) }) |
| 297 | + _copy })) |
| 298 | + |
278 | 299 | (let _valAsJson (fun (_val) |
279 | 300 | (if (= "Dict" (type _val)) |
280 | 301 | (asJson _val) |
|
0 commit comments