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

Previous Next

Package: emacs;

Reported by: Okam <okamsn <at> protonmail.com>

Date: Sun, 4 Jul 2021 23:10:01 UTC

Severity: wishlist

Fixed in version 30.1

Done: "Basil L. Contovounesios" <contovob <at> tcd.ie>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Okam <okamsn <at> protonmail.com>
Cc: Lars Ingebrigtsen <larsi <at> gnus.org>, 49407 <at> debbugs.gnu.org, Nicolas Petton <nicolas <at> petton.fr>, Stefan Monnier <monnier <at> 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
Okam <okamsn <at> 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.




This bug report was last modified 2 years and 2 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.