From: Adam Porter <adam@alphapapa.net>
To: emacs-devel@gnu.org
Subject: Re: map.el: pcase plist keyword-only binding improvement
Date: Sun, 02 Feb 2020 10:38:56 -0600 [thread overview]
Message-ID: <874kw92bbz.fsf@alphapapa.net> (raw)
In-Reply-To: jwvlfpl85vg.fsf-monnier+emacs@gnu.org
[-- Attachment #1: Type: text/plain, Size: 1395 bytes --]
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> (seq-map (lambda (elt)
>> (cond ((consp elt)
>> `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt)))
>> ((keywordp elt)
>> ;; New clause for keyword-only elements.
>> (let ((var (intern (substring (symbol-name elt) 1))))
>> `(app (pcase--flip map-elt ,elt) ,var)))
>> (t
>> `(app (pcase--flip map-elt ',elt) ,elt))))
>> args))
>
> Since binding the result of the match to a keyword can't work anyway,
> this seems like a very good idea.
>
>> Assuming that this change is acceptable, I would like to prepare a
>> patch, which would probably need to include NEWS and documentation
>> changes.
>
> AFAICT we don't yet have actual documentation for the map.el package, so
> only NEWS needs to be changed.
>
>> However, since the change is to map.el rather than pcase.el,
>> and since map.el is also available on ELPA,
>
> It's also distributed via GNU ELPA indeed, but taken straight from the
> emacs.git repository, so you don't need to worry about that (just
> increment the `Version:` entry at the top).
Hi Stefan,
Ok, I've attached a patch. I tried to follow the appropriate
guidelines, but please let me know if any changes are required.
Thanks,
Adam
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: map.el keyword-only pattern abbreviation --]
[-- Type: text/x-diff, Size: 3465 bytes --]
From d5cb310d6fb30e2991a94e433b754c07893133ef Mon Sep 17 00:00:00 2001
From: Adam Porter <adam@alphapapa.net>
Date: Sun, 2 Feb 2020 10:17:20 -0600
Subject: [PATCH] * lisp/emacs-lisp/map.el: Add keyword-only pattern
abbreviation
* lisp/emacs-lisp/map.el: Update version to 2.1.
((pcase-defmacro map)): Update docstring.
(map--make-pcase-bindings): Match keyword pattern.
* test/lisp/emacs-lisp/map-tests.el (test-map-plist-pcase): Add test.
---
etc/NEWS | 6 ++++++
lisp/emacs-lisp/map.el | 17 +++++++++++------
test/lisp/emacs-lisp/map-tests.el | 6 ++++++
3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/etc/NEWS b/etc/NEWS
index 2b9337b..7444166 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -107,6 +107,12 @@ supplied error message.
*** New connection method "media", which allows accessing media devices
like cell phones, tablets or cameras.
+** map.el
+
+*** Pcase 'map' pattern added keyword symbols abbreviation.
+A pattern like '(map :sym)' binds the map's value for ':sym' to 'sym',
+equivalent to '(map (:sym sym))'.
+
\f
* New Modes and Packages in Emacs 28.1
diff --git a/lisp/emacs-lisp/map.el b/lisp/emacs-lisp/map.el
index 67f5b3c..9c23344 100644
--- a/lisp/emacs-lisp/map.el
+++ b/lisp/emacs-lisp/map.el
@@ -4,7 +4,7 @@
;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: convenience, map, hash-table, alist, array
-;; Version: 2.0
+;; Version: 2.1
;; Package-Requires: ((emacs "25"))
;; Package: map
@@ -56,8 +56,10 @@ 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 to the KEY.
-Each element can also be a SYMBOL, which is an abbreviation of a (KEY
-PAT) tuple of the form (\\='SYMBOL SYMBOL).
+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."
@@ -486,9 +488,12 @@ Example:
(defun map--make-pcase-bindings (args)
"Return a list of pcase bindings from ARGS to the elements of a map."
(seq-map (lambda (elt)
- (if (consp elt)
- `(app (pcase--flip map-elt ,(car elt)) ,(cadr elt))
- `(app (pcase--flip map-elt ',elt) ,elt)))
+ (cond ((consp 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)))
+ (t `(app (pcase--flip map-elt ',elt) ,elt))))
args))
(defun map--make-pcase-patterns (args)
diff --git a/test/lisp/emacs-lisp/map-tests.el b/test/lisp/emacs-lisp/map-tests.el
index 06fd55f..3ffef17 100644
--- a/test/lisp/emacs-lisp/map-tests.el
+++ b/test/lisp/emacs-lisp/map-tests.el
@@ -376,5 +376,11 @@ Evaluate BODY for each created map.
'((1 . 1) (2 . 5) (3 . 0)))
'((3 . 0) (2 . 9) (1 . 6)))))
+(ert-deftest test-map-plist-pcase ()
+ (let ((plist '(:one 1 :two 2)))
+ (should (equal (pcase-let (((map :one (:two two)) plist))
+ (list one two))
+ '(1 2)))))
+
(provide 'map-tests)
;;; map-tests.el ends here
--
2.7.4
next prev parent reply other threads:[~2020-02-02 16:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-02 9:37 map.el: pcase plist keyword-only binding improvement Adam Porter
2020-02-02 13:46 ` Stefan Monnier
2020-02-02 16:38 ` Adam Porter [this message]
[not found] ` <874kw86dft.fsf@web.de>
2020-02-03 15:02 ` Nicolas Petton
2020-02-04 17:30 ` Stefan Monnier
2020-02-04 22:31 ` Adam Porter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=874kw92bbz.fsf@alphapapa.net \
--to=adam@alphapapa.net \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.