Function: let-alist
let-alist is an autoloaded macro defined in let-alist.el.gz.
Signature
(let-alist ALIST &rest BODY)
Documentation
Let-bind dotted symbols to their cdrs in ALIST and execute BODY.
Dotted symbol is any symbol starting with a .. This macro creates
let-bindings for dotted symbols that appear literally in BODY (whether
or not they are actually used). It does not create bindings for dotted
symbols that are introduced by macro-expansion in BODY.
A symbol of the form .foo.N where N is a natural number refers to the
Nth element of the value that ALIST associates to key foo.
For instance, the following code
(let-alist alist
(if (and .title.0 .body)
.body
.site
.site.contents))
essentially expands to
(let ((.title.0 (nth 0 (cdr (assq 'title alist))))
(.body (cdr (assq 'body alist)))
(.site (cdr (assq 'site alist)))
(.site.contents (cdr (assq 'contents (cdr (assq 'site alist))))))
(if (and .title.0 .body)
.body
.site
.site.contents))
If you nest let-alist invocations, the inner one can't access
the variables of the outer one. You can, however, access alists
inside the original alist by using dots inside the symbol, as
displayed in the example above.
To refer to a non-let-alist variable starting with a dot in BODY, use
two dots instead of one. For example, in the following form ..foo
refers to the variable .foo bound outside of the let-alist:
(let ((.foo 42)) (let-alist '((foo . nil)) ..foo))
Note that there is no way to differentiate the case where a key is missing from when it is present, but its value is nil. Thus, the following form evaluates to nil:
(let-alist '((some-key . nil))
.some-key)
Other relevant functions are documented in the alist group.
Probably introduced at or before Emacs version 25.1.
Shortdoc
;; alist
(let ((colors '((rose . red) (lily . white)))) (let-alist colors (if (eq .rose 'red) .lily)))
=> white
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/let-alist.el.gz
;;; The actual macro.
;;;###autoload
(defmacro let-alist (alist &rest body)
"Let-bind dotted symbols to their cdrs in ALIST and execute BODY.
Dotted symbol is any symbol starting with a `.'. This macro creates
let-bindings for dotted symbols that appear literally in BODY (whether
or not they are actually used). It does not create bindings for dotted
symbols that are introduced by macro-expansion in BODY.
A symbol of the form `.foo.N' where N is a natural number refers to the
Nth element of the value that ALIST associates to key `foo'.
For instance, the following code
(let-alist alist
(if (and .title.0 .body)
.body
.site
.site.contents))
essentially expands to
(let ((.title.0 (nth 0 (cdr (assq \\='title alist))))
(.body (cdr (assq \\='body alist)))
(.site (cdr (assq \\='site alist)))
(.site.contents (cdr (assq \\='contents (cdr (assq \\='site alist))))))
(if (and .title.0 .body)
.body
.site
.site.contents))
If you nest `let-alist' invocations, the inner one can't access
the variables of the outer one. You can, however, access alists
inside the original alist by using dots inside the symbol, as
displayed in the example above.
To refer to a non-`let-alist' variable starting with a dot in BODY, use
two dots instead of one. For example, in the following form `..foo'
refers to the variable `.foo' bound outside of the `let-alist':
(let ((.foo 42)) (let-alist \\='((foo . nil)) ..foo))
Note that there is no way to differentiate the case where a key
is missing from when it is present, but its value is nil. Thus,
the following form evaluates to nil:
(let-alist \\='((some-key . nil))
.some-key)"
(declare (indent 1) (debug t))
(let ((var (make-symbol "alist")))
`(let ((,var ,alist))
(let ,(mapcar (lambda (x) `(,(car x) ,(let-alist--access-sexp (car x) var)))
(delete-dups (let-alist--deep-dot-search body)))
,@body))))