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

From: Okam <[email-masked]>
Date: Sat, 29 Jul 2023 00:37:38 +0000

Basil L. Contovounesios wrote:
> Okam via "Bug reports for GNU Emacs, the Swiss army knife of text editors" [2023-07-27 01:39 +0000] wrote:
> 
>>  From 99208ca640d4944c57082772702cb8534fe91d32 Mon Sep 17 00:00:00 2001
>> From: Earl Hyatt <[email-masked]>
> 
> I think your past contributions were under "Earl Hyatt
> <[email-masked]>", right?  Is [email-masked] your current
> address?  Eli, does it make a difference for the CA, do we need to
> update .mailmap, or anything like that?

Yes, I changed addresses. The mailmap makes it look like I used the old 
address in `git log`. Would it be OK to make it so that the old address 
shows as the new one instead?

>> Date: Thu, 20 Jul 2023 21:44:41 -0400
>> Subject: [PATCH v3] 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
>                                               ^^
>                                               Two spaces here.

Fixed.

>> +Each element in ARGS can be of the form (KEY PAT) or (KEY PAT
>> +DEFAULT), where KEY is the key sought in the map, PAT is a
>> +`pcase' pattern, and DEFAULT is the default value to use if KEY
>> +isn't found.  Both KEY and DEFAULT are evaluated.  If DEFAULT
>> +isn't given, then nil is used.
> 
> FWIW, the form/structure can be kept on a single line, and a couple of
> passive voices avoided, by rewording as e.g.
> 
>    Each element of ARGS can be of the form (KEY PAT [DEFAULT]),
>    which looks up KEY in the map and matches the associated value
>    against `pcase' pattern PAT.  DEFAULT specifies the fallback
>    value to use when KEY is not present in the map.  If omitted, it
>    defaults to nil.  Both KEY and DEFAULT are evaluated.

I have changed it to this.

>> +The match fails if:
>> +
>> +- KEY is found and PAT does not match the associated value
>> +
>> +- KEY is not found, DEFAULT is given, and PAT does not match
>> +  DEFAULT
>> +
>> +- KEY is not found, DEFAULT is not given, and PAT does not match
>> +  nil"
> 
> I find this partly redundant/repetitive, since we already know that PAT
> is meant to match either KEY's value (when found), or DEFAULT (which
> defaults to nil).
> 
> What this doesn't make explicit (compared to the previous version) is
> what happens when there are multiple ARGS; specifically, when one or
> more of them don't match.

I changed it to "An element of ARGS fails to match if PAT does not match 
the associated value or the default value.  The overall pattern fails
to match if any element of ARGS fails to match." Does that work?

>> +KEYS can also be a list of (KEY VARNAME) pairs and
>> +\(KEY VARNAME DEFAULT) triples, in which case KEY and
> 
> This could also use (KEY VARNAME [DEFAULT]) syntax, if preferred.

Changed.

Thank you.

From 74616d0717c520747328587cf1551399eb11a318 Mon Sep 17 00:00:00 2001
From: Earl Hyatt <[email-masked]>
Date: Thu, 20 Jul 2023 21:44:41 -0400
Subject: [PATCH v4] 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            | 33 +++++++++++------
 test/lisp/emacs-lisp/map-tests.el | 59 +++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 10 deletions(-)

diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 7a48ba47434..b55eb431668 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -50,18 +50,20 @@ 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 [DEFAULT]),
+which looks up KEY in the map and matches the associated value
+against `pcase' pattern PAT.  DEFAULT specifies the fallback
+value to use when KEY is not present in the map.  If omitted, it
+defaults to nil.  Both KEY and DEFAULT are evaluated.
 
 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."
+An element of ARGS fails to match if PAT does not match the
+associated value or the default value.  The overall pattern fails
+to match if any element of ARGS fails to match."
   `(and (pred mapp)
         ,@(map--make-pcase-bindings args)))
 
@@ -71,12 +73,13 @@ 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 [DEFAULT]) sublists, in
+which case KEY and DEFAULT are unquoted forms.
 
 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))
 
@@ -595,11 +598,21 @@ map-into
     (map-into \\='((1 . 3)) \\='(hash-table :test eql))"
   (map--into-hash map (cdr type)))
 
+(defmacro map--pcase-map-elt (key default map)
+  "A macro to make MAP the last argument to `map-elt'.
+
+This allows using default values for `map-elt', which can't be
+done using `pcase--flip'.
+
+KEY is the key sought in the map.  DEFAULT is the default value."
+  `(map-elt ,map ,key ,default))
+
 (defun map--make-pcase-bindings (args)
   "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)))
+                   `(app (map--pcase-map-elt ,(car elt) ,(caddr 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..2204743f794 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 (equal a 1))
+    (should (equal 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,58 @@ 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))))
+
+    (should (equal 'fail
+                   (pcase plist
+                     ((map (:two two 33)
+                           (:three `(,a . ,b)))
+                      (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