Skip to content
Snippets Groups Projects
Commit 2e55bfea authored by Alexander Fedorov's avatar Alexander Fedorov
Browse files

Add :default key argument for assoc-value and rassoc-value

parent 8514d8e6
No related branches found
No related tags found
No related merge requests found
Pipeline #10941 failed
...@@ -30,11 +30,11 @@ property list PLIST in the same order." ...@@ -30,11 +30,11 @@ property list PLIST in the same order."
((define-alist-get (name get-entry get-value-from-entry add doc) ((define-alist-get (name get-entry get-value-from-entry add doc)
`(progn `(progn
(declaim (inline ,name)) (declaim (inline ,name))
(defun ,name (alist key &key (test 'eql)) (defun ,name (alist key &key (test 'eql) default)
,doc ,doc
(let ((entry (,get-entry key alist :test test))) (let ((entry (,get-entry key alist :test test)))
(values (,get-value-from-entry entry) entry))) (values (if entry (,get-value-from-entry entry) default) entry)))
(define-setf-expander ,name (place key &key (test ''eql) (define-setf-expander ,name (place key &key (test ''eql) default
&environment env) &environment env)
(multiple-value-bind (multiple-value-bind
(temporary-variables initforms newvals setter getter) (temporary-variables initforms newvals setter getter)
...@@ -62,7 +62,7 @@ property list PLIST in the same order." ...@@ -62,7 +62,7 @@ property list PLIST in the same order."
(setf ,(first newvals) (,',add ,key ,new-value ,alist)) (setf ,(first newvals) (,',add ,key ,new-value ,alist))
,setter ,setter
,new-value))) ,new-value)))
`(,',get-value-from-entry ,entry)))))))) `(if ,entry (,',get-value-from-entry ,entry) ,default))))))))
(define-alist-get assoc-value assoc cdr acons (define-alist-get assoc-value assoc cdr acons
"ASSOC-VALUE is an alist accessor very much like ASSOC, but it can "ASSOC-VALUE is an alist accessor very much like ASSOC, but it can
be used with SETF.") be used with SETF.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment