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

From: Okam <[email-masked]>
Date: Fri, 21 Jul 2023 02:56:19 +0000

On 2021-07-15 08:51 UTC, Lars Ingebrigtsen wrote:
> Okam <[email-masked]> writes:
> 
>> `map-let` allows one to conveniently bind variables using `map-elt`, but
>> does not provide a way specify a default value if a key is missing.
>>
>> With `map-elt`, one can use the optional third argument to specify this
>> value.  It would be good to have this in `map-let` as well.
>>
>> For example, maybe it could look something like
>>
>>       ;; As just a third value in the list:
>>       (let ((map '(:a 1 :b 2)))
>>         (map-let ((:a a)
>>                   (:b b)
>>                   (:c c 3))
>>             map
>>           (+ a b c)))
> 
> Hm...  I guess that could be useful.  I've added Nicolas to the CCs;
> perhaps he has an opinion here.
> 
> --
> (domestic pets only, the antidote for overdose, milk.)
>     bloggy blog: http://lars.ingebrigtsen.no

Hello,

I've written a patch and tests that would add an optional third argument 
for a default value. I also changed the documentation string for the 
pcase pattern, which said that unfound keys are ignored and won't cause 
the pattern to fail. That didn't seem true for patterns that didn't 
match nil. For example, this won't match:

(pcase '(:two 2)
   ((map (:two two 33)
         (:three `(,a . ,b)))
    (list two a b))
   (_ 'fail))

However, I have a question about avoiding using a lambda. I see that the 
pattern uses `pcase--flip`, which is "used internally to avoid (funcall 
(lambda ...) ...)". Why should lambda functions be avoided for this, and 
I should be using a custom helper function for this one pattern?

Thank you.

From 1dbb797a9a736fc81ad9f672da815e2510e7f31a Mon Sep 17 00:00:00 2001
From: Earl Hyatt <[email-masked]>
Date: Thu, 20 Jul 2023 21:44:41 -0400
Subject: [PATCH] Allow default values in 'map-let' and the pcase 'map' form

* lisp/emacs-lisp/map.el (map-let, map)
(map--make-pcase-bindings): Add a third argument for specifying a
default value, like in 'map-elt'. (Bug#49407)

* lisp/emacs-lisp/map.el (map--make-pcase-bindings): Clarify that keys
that aren't found aren't ignored, they actually get the value
nil (unless the new default value is given). The overall pattern can
still fail to match if the sub-pattern for the unfound key doesn't
match nil.

* test/lisp/emacs-lisp/map-tests.el (test-map-let-default)
(test-map-plist-pcase-default, test-map-pcase-matches): Add tests,
including for the above item.
---
 lisp/emacs-lisp/map.el            | 31 ++++++++++++------
 test/lisp/emacs-lisp/map-tests.el | 52 +++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 10 deletions(-)

diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 7a48ba47434..7f127f312de 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -50,18 +50,23 @@ map
 
 ARGS is a list of elements to be matched in the map.
 
-Each element of ARGS can be of the form (KEY PAT), in which case KEY is
-evaluated and searched for in the map.  The match fails if for any KEY
-found in the map, the corresponding PAT doesn't match the value
-associated with the KEY.
+Each element of ARGS can be of the form (KEY PAT) or (KEY PAT DEFAULT),
+
+in which case KEY is evaluated and searched for in the map and
+DEFAULT is the evaluated value used if KEY is not found.  The
+match fails if for any KEY found in the map, the corresponding
+PAT doesn't match the value associated with the KEY.  The match
+fails if for any KEY not found in the map, the corresponding PAT
+doesn't match the DEFAULT value, if given.
 
 Each element can also be a SYMBOL, which is an abbreviation of
 a (KEY PAT) tuple of the form (\\='SYMBOL SYMBOL).  When SYMBOL
 is a keyword, it is an abbreviation of the form (:SYMBOL SYMBOL),
 useful for binding plist values.
 
-Keys in ARGS not found in the map are ignored, and the match doesn't
-fail."
+When no DEFAULT value is given, the default value for keys in
+ARGS not found in the map is nil, and the match doesn't fail so
+long at PAT, if given, matches nil."
   `(and (pred mapp)
         ,@(map--make-pcase-bindings args)))
 
@@ -71,12 +76,14 @@ map-let
 KEYS can be a list of symbols, in which case each element will be
 bound to the looked up value in MAP.
 
-KEYS can also be a list of (KEY VARNAME) pairs, in which case
-KEY is an unquoted form.
+KEYS can also be a list of (KEY VARNAME) pairs and
+\(KEY VARNAME DEFAULT) triples, in which case KEY is an
+unquoted form.
 
 MAP can be an alist, plist, hash-table, or array."
   (declare (indent 2)
-           (debug ((&rest &or symbolp ([form symbolp])) form body)))
+           (debug ((&rest &or symbolp ([form symbolp &optional form]))
+                   form body)))
   `(pcase-let ((,(map--make-pcase-patterns keys) ,map))
      ,@body))
 
@@ -599,7 +606,11 @@ map--make-pcase-bindings
   "Return a list of pcase bindings from ARGS to the elements of a map."
   (mapcar (lambda (elt)
             (cond ((consp elt)
-                   `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt)))
+                   (if (cddr elt)
+                       `(app (lambda (arg)
+                               (map-elt arg ,(car elt) ,(caddr elt)))
+                             ,(cadr elt))
+                     `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))))
                   ((keywordp elt)
                    (let ((var (intern (substring (symbol-name elt) 1))))
                      `(app (pcase--flip map-elt ,elt) ,var)))
diff --git a/test/lisp/emacs-lisp/map-tests.el b/test/lisp/emacs-lisp/map-tests.el
index 86c0e9e0503..42458fbfeb7 100644
--- a/test/lisp/emacs-lisp/map-tests.el
+++ b/test/lisp/emacs-lisp/map-tests.el
@@ -577,6 +577,13 @@ test-map-let
     (should (= b 2))
     (should-not c)))
 
+(ert-deftest test-map-let-default ()
+  (map-let (('foo a 3)
+            ('baz b 4))
+      '((foo . 1))
+    (should (= a 1))
+    (should (= b 4))))
+
 (ert-deftest test-map-merge ()
   "Test `map-merge'."
   (should (equal (sort (map-merge 'list '(a 1) '((b . 2) (c . 3))
@@ -617,6 +624,51 @@ test-map-plist-pcase
                      (list one two))
                    '(1 2)))))
 
+(ert-deftest test-map-plist-pcase-default ()
+  (let ((plist '(:two 2)))
+    (should (equal (pcase-let (((map (:two two 33)
+                                     (:three three 44))
+                                plist))
+                     (list two three))
+                   '(2 44)))))
+
+(ert-deftest test-map-pcase-matches ()
+  (let ((plist '(:two 2)))
+    (should (equal (pcase plist
+                     ((map (:two two 33)
+                           (:three three))
+                      (list two three))
+                     (_ 'fail))
+                   '(2 nil)))
+
+    (should (equal (pcase plist
+                     ((map (:two two 33)
+                           (:three three 44))
+                      (list two three))
+                     (_ 'fail))
+                   '(2 44)))
+
+    (should (equal (pcase plist
+                     ((map (:two two 33)
+                           (:three `(,a . ,b) '(11 . 22)))
+                      (list two a b))
+                     (_ 'fail))
+                   '(2 11 22)))
+
+    (should (equal 'fail
+                   (pcase plist
+                     ((map (:two two 33)
+                           (:three `(,a . ,b) 44))
+                      (list two a b))
+                     (_ 'fail))))
+
+    (should (equal 'fail
+                   (pcase plist
+                     ((map (:two two 33)
+                           (:three `(,a . ,b) nil))
+                      (list two a b))
+                     (_ 'fail))))))
+
 (ert-deftest test-map-setf-alist-insert-key ()
   (let ((alist))
     (should (equal (setf (map-elt alist 'key) 'value)
-- 
2.34.1