bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#49407: Request: Specify default values in `map-let` in Map.el


From: Okam
Subject: bug#49407: Request: Specify default values in `map-let` in Map.el
Date: Fri, 16 Jul 2021 01:45:24 +0000

On 7/15/21 4:51 AM, Lars Ingebrigtsen wrote:
> Hm...  I guess that could be useful.  I've added Nicolas to the CCs;
> perhaps he has an opinion here.

If it helps, below is a version of the idea that I am using, specific to
plists:

(defmacro plist-bind (bindings plist &rest body)
   "Bind values in PLIST to variables in BINDINGS, surrounding BODY.

- PLIST is a property list.

- BINDINGS is of the form (KEY VAR KEY VAR ...).  VAR can
   optionally be a list of two elements: a variable name and a
   default value, similar to what one would use for expressing
   keyword parameters in `cl-defun' or `cl-destructuring-bind'.
   The default value is used /only/ when KEY is not found in
   PLIST.

- BODY is the same as in `let'.

This macro works the same as `cl-destructuring-bind', except for
the case when keys exist in PLIST that are not listed in
BINDINGS.  While `cl-destructuring-bind' would signal an error,
this macro simply ignores them."
   (declare (indent 2))
   (let ((value-holder (gensym "plist-let-"))
         (found-key (gensym "plist-prop-found-")))
     `(let* ((,value-holder ,plist)
             ,@(cl-loop for (key var . _) on bindings by #'cddr
                        if (consp var)
                        collect `(,(cl-first var)
                                  ;; Use `plist-member' instead of
`plist-get' to
                                  ;; allow giving `nil' as an argument
without
                                  ;; using the default value.
                                  (if-let ((,found-key (plist-member
,value-holder
                                                                     ,key)))
                                      (cl-second ,found-key)
                                    ,(cl-second var)))
                        else collect `(,var (plist-get ,value-holder
,key))))
        ,@body)))








reply via email to

[Prev in Thread] Current Thread [Next in Thread]