unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* map.el: pcase plist keyword-only binding improvement
@ 2020-02-02  9:37 Adam Porter
  2020-02-02 13:46 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Adam Porter @ 2020-02-02  9:37 UTC (permalink / raw)
  To: emacs-devel

Hi,

One of the features I find very useful in dash.el's `-let' macro is its
ability to bind plist values to variables of the same names as the keys.
With map.el's `pcase' support, Emacs's `pcase' and `pcase-let' can do
this as well, but in a less convenient way, because it requires writing
both the keyword and the variable name.

However, with a simple change to one of map'el's functions, it can
support keyword-only plist binding as well.

Example usage before change:

#+BEGIN_SRC elisp
  (let ((pl '(:one 1 :two 2)))
    (pcase pl
      ((map :one (:two two))
       (list one two))))  ;;=> nil

  (pcase-let* (((map :one (:two two))
                '(:one 1 :two 2)))
    (list one two)) ;;=> (void-variable one)
#+END_SRC

Changed function:

#+BEGIN_SRC elisp
  (defun map--make-pcase-bindings (args)
    "Return a list of pcase bindings from ARGS to the elements of a map."
    (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))
#+END_SRC

Example usage after change:

#+BEGIN_SRC elisp
  (let ((pl '(:one 1 :two 2)))
    (pcase pl
      ((map :one (:two two))
       (list one two)))) ;;=> (1 2)

  (pcase-let* (((map :one (:two two))
                '(:one 1 :two 2)))
    (list one two)) ;;=> (1 2)
#+END_SRC

Assuming that this change is acceptable, I would like to prepare a
patch, which would probably need to include NEWS and documentation
changes.  However, since the change is to map.el rather than pcase.el,
and since map.el is also available on ELPA, I'm not sure how to proceed
with those parts, where to make the appropriate documentation changes,
etc.

Please let me know if this proposal is acceptable and how I should
proceed.

Thanks,
Adam




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-02-04 22:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
     [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

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).