feat(wpcarro/emacs): Support default value for al-get

Similar to Elixir's `Map.get/3`.

Change-Id: I736f7e618aafc786a32eeba46ca635cb27db18b6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6000
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
This commit is contained in:
William Carroll 2022-07-29 18:12:16 -07:00 committed by clbot
parent 230c4bbb3e
commit 6b3f4113cc
2 changed files with 18 additions and 3 deletions

View file

@ -111,10 +111,12 @@ Note that this doesn't append to the alist in the way that most alists handle
(map-put! xs k v))
;; Read
(defun al-get (k xs)
"Return the value at K in XS; otherwise, return nil.
(defun al-get (k xs &optional default)
"Return the value at K in XS; otherwise, return nil or DEFAULT (if set).
Returns the first occurrence of K in XS since alists support multiple entries."
(cdr (assoc k xs)))
(if (not (al-has-key? k xs))
default
(cdr (assoc k xs))))
(defun al-get-entry (k xs)
"Return the first key-value pair at K in XS."