Skip to content

Commit 2303e11

Browse files
committed
Add assoc-in for nested structures
1 parent 60cfc95 commit 2303e11

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/algorithm/hamt.lisp

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
(:export #:+empty+
1818
#:immutable-map
1919
#:assoc
20+
#:assoc-in
2021
#:contains-p
2122
#:difference
2223
#:dissoc
@@ -422,6 +423,16 @@ Returns (values new-node inserted-p) where inserted-p is true for new insertions
422423
(make-immutable-map new-root (+ (immutable-map-count map)
423424
(if inserted 1 0))))))
424425

426+
(defun assoc-in (map keys value)
427+
"Associate VALUE in a nested MAP structure, where KEYS is a sequence of keys."
428+
(if (null keys)
429+
value
430+
(let ((k (car keys)))
431+
(assoc map k
432+
(assoc-in (get map k +empty+)
433+
(cdr keys)
434+
value)))))
435+
425436
(defun reduce (function map &optional (initial-value +empty+))
426437
"Reduce over key-value pairs in the map"
427438
(cl:reduce (lambda (acc pair)

0 commit comments

Comments
 (0)