unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Okam <okamsn@protonmail.com>
Cc: Lars Ingebrigtsen <larsi@gnus.org>,
	49407@debbugs.gnu.org, Nicolas Petton <nicolas@petton.fr>,
	Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#49407: Request: Specify default values in `map-let` in Map.el
Date: Sat, 22 Jul 2023 03:48:03 +0200	[thread overview]
Message-ID: <87r0p0wwbw.fsf@web.de> (raw)
In-Reply-To: <08a00b74-56f9-bb81-0411-36f9da8cf2d6@protonmail.com> (Okam's message of "Fri, 21 Jul 2023 02:56:19 +0000")

Okam <okamsn@protonmail.com> writes:

> I've written a patch and tests that would add an optional third
> argument for a default value.

Thank you.

> 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,

As far as I understand, to produce more efficient code.

> and I should be using a custom helper function for this one pattern?

Stefan, would you want to extend `pcase--flip' to more arguments to
support this case?


| 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

That line break in the last sentence is somehow... broken.

| +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.

Here is something grammatically not right in the last sentence.  And I
think we should say that that last case (PAT doesn't match DEFAULT) only
lets matching fail when the KEY is not found.


| -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)))

Typo in doc ("at").  And can't we simply say that omitting DEFAULT is
equivalent to a DEFAULT of nil?

| @@ -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))))

If we don't extend `pcase--flip' to support this case, I think your
lambda would need to use an uninterned symbol as parameter to avoid
collisions with the involved expressions.


Michael.





  reply	other threads:[~2023-07-22  1:48 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-04 23:08 bug#49407: Request: Specify default values in `map-let` in Map.el Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-07-15  8:51 ` Lars Ingebrigtsen
2021-07-16  1:45   ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-21  2:56   ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-22  1:48     ` Michael Heerdegen [this message]
2023-07-22 15:45       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-22 18:46       ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-26  3:41         ` Michael Heerdegen
2023-07-27  1:39           ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-28 18:02             ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-28 19:19               ` Eli Zaretskii
2023-07-29  0:37               ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-30 13:53                 ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-31  4:02                   ` Michael Heerdegen
2023-08-08 12:41                     ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-06 13:31                   ` Okam via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-08 12:46                     ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-07-27  1:37 ` Earl Hyatt via Bug reports for GNU Emacs, the Swiss army knife of text editors

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r0p0wwbw.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=49407@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=nicolas@petton.fr \
    --cc=okamsn@protonmail.com \
    /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 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).