unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
       [not found] ` <E1YzsfE-0005vH-4d@vcs.savannah.gnu.org>
@ 2015-06-03  1:06   ` Stefan Monnier
  2015-06-04 20:18     ` Nicolas Petton
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2015-06-03  1:06 UTC (permalink / raw)
  To: emacs-devel; +Cc: Nicolas Petton

> +  "pcase pattern matching map elements.
> +Matches if the object is a map (list, hash-table or array), and
> +binds values from ARGS to the corresponding element of the map.
> +
> +ARGS can be an alist of key/binding pairs of a list of keys."

I think this would benefit from a bit more detail.  E.g. you could say
"ARGS is a list of elements of the form (KEY PAT)" or something
like that.

> +  (seq-map (lambda (elt)
> +             (if (consp elt)
> +                 `(app (pcase--flip map-elt ',(car elt)) ,(cdr elt))
> +               `(app (pcase--flip map-elt ',elt) ,elt)))
> +           args))

Hmm... It looks like it's actually elements of the form (KEY . PAT) or
elements of the form SYMBOL which stands for (SYMBOL . SYMBOL).

Some alternatives to consider:
- Use (KEY PAT) instead of (KEY . PAT).  This makes the source code
  a bit more concise, so I'm in favor of the (KEY PAT) form.
- Use a plist rather than an alist.  I like alists better in general, so
  I'm fine with this choice.
- let KEY be evaluated rather than having it be quoted.  I.e. require
  the programmer to write 'a when looking up the key `a'.  It costs an
  extra quote in some/many cases, but does give you extra power.
  Of course, the downside is that it begs the question of what to do
  with the SYMBOL case.

FWIW, in dash.el, the `-let' includes a similar destructuring
facility, with patterns of the form:

(&plist key0 a0 ... keyN aN) - bind value mapped by keyK in the
                               `source` plist to aK.  If the
                               value is not found, aK is nil.

(&alist key0 a0 ... keyN aN) - bind value mapped by keyK in the
                               `source` alist to aK.  If the
                               value is not found, aK is nil.

(&hash key0 a0 ... keyN aN) - bind value mapped by keyK in the
                              `source` hash table to aK.  If the
                              value is not found, aK is nil.

where, AFAICT the `key's are expressions which are evaluated before
being passed to the respective lookup function.  IOW when you want
a constant key you need to quote it, unless that constant is
self-quoting (e.g. a keyword).


        Stefan



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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-03  1:06   ` [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it Stefan Monnier
@ 2015-06-04 20:18     ` Nicolas Petton
  2015-06-04 22:39       ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Petton @ 2015-06-04 20:18 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Nicolas Petton, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1628 bytes --]


Stefan Monnier writes:

>> +  "pcase pattern matching map elements.
>> +Matches if the object is a map (list, hash-table or array), and
>> +binds values from ARGS to the corresponding element of the map.
>> +
>> +ARGS can be an alist of key/binding pairs of a list of keys."
>
> I think this would benefit from a bit more detail.  E.g. you could say
> "ARGS is a list of elements of the form (KEY PAT)" or something
> like that.

Yes, and there's a typo. It should be "[...] or a list of keys.".

>
>> +  (seq-map (lambda (elt)
>> +             (if (consp elt)
>> +                 `(app (pcase--flip map-elt ',(car elt)) ,(cdr elt))
>> +               `(app (pcase--flip map-elt ',elt) ,elt)))
>> +           args))
>
> Hmm... It looks like it's actually elements of the form (KEY . PAT) or
> elements of the form SYMBOL which stands for (SYMBOL . SYMBOL).

Yes, exactly.

> Some alternatives to consider:
> - Use (KEY PAT) instead of (KEY . PAT).  This makes the source code
>   a bit more concise, so I'm in favor of the (KEY PAT) form.

Why not, I don't have a strong opinion, and I think both would work.

> - Use a plist rather than an alist.  I like alists better in general, so
>   I'm fine with this choice.

Yes, I think it's better than a plist.

> - let KEY be evaluated rather than having it be quoted.  I.e. require
>   the programmer to write 'a when looking up the key `a'.  It costs an
>   extra quote in some/many cases, but does give you extra power.

Do you think it would be worth it? I'm not sure.

Cheers,
Nico
-- 
Nicolas Petton
http://nicolas-petton.fr

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-04 20:18     ` Nicolas Petton
@ 2015-06-04 22:39       ` Stefan Monnier
  2015-06-05  8:09         ` Nicolas Petton
  2015-06-07 18:36         ` Michael Heerdegen
  0 siblings, 2 replies; 7+ messages in thread
From: Stefan Monnier @ 2015-06-04 22:39 UTC (permalink / raw)
  To: Nicolas Petton; +Cc: emacs-devel

>> - let KEY be evaluated rather than having it be quoted.  I.e. require
>> the programmer to write 'a when looking up the key `a'.  It costs an
>> extra quote in some/many cases, but does give you extra power.
> Do you think it would be worth it? I'm not sure.

Not sure either; it will clearly depend on actual uses.
This said, if the main case where the quote would be needed is when KEY
is a (non-keyword) symbol, then it's probably worth the extra
flexibility since you could say that an element of the form SYMBOL is
treated as equivalent to ('SYMBOL SYMBOL), so the extra quote will be
very rarely needed.


        Stefan



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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-04 22:39       ` Stefan Monnier
@ 2015-06-05  8:09         ` Nicolas Petton
  2015-06-07 18:36         ` Michael Heerdegen
  1 sibling, 0 replies; 7+ messages in thread
From: Nicolas Petton @ 2015-06-05  8:09 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Nicolas Petton, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 739 bytes --]


Stefan Monnier writes:

>>> - let KEY be evaluated rather than having it be quoted.  I.e. require
>>> the programmer to write 'a when looking up the key `a'.  It costs an
>>> extra quote in some/many cases, but does give you extra power.
>> Do you think it would be worth it? I'm not sure.
>
> Not sure either; it will clearly depend on actual uses.
> This said, if the main case where the quote would be needed is when KEY
> is a (non-keyword) symbol, then it's probably worth the extra
> flexibility since you could say that an element of the form SYMBOL is
> treated as equivalent to ('SYMBOL SYMBOL), so the extra quote will be
> very rarely needed.

Good point.

Nico
-- 
Nicolas Petton
http://nicolas-petton.fr

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-04 22:39       ` Stefan Monnier
  2015-06-05  8:09         ` Nicolas Petton
@ 2015-06-07 18:36         ` Michael Heerdegen
  2015-06-08 14:16           ` Stefan Monnier
  1 sibling, 1 reply; 7+ messages in thread
From: Michael Heerdegen @ 2015-06-07 18:36 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Nicolas Petton, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> This said, if the main case where the quote would be needed is when KEY
> is a (non-keyword) symbol, then it's probably worth the extra
> flexibility since you could say that an element of the form SYMBOL is
> treated as equivalent to ('SYMBOL SYMBOL), so the extra quote will be
> very rarely needed.

Now that this has been finished, we should add to the doc what KEY _is_
in the "elements of the form (KEY PAT)", and how it is compared
(e.g. say it's interpreted as an expression to be evaluated, and
compared with "equal" in the alist case).

Perhaps we should also say that only one occurrence of KEY is tested in
the alist case (is that true?), e.g.

(pcase '((1 . 11) (1 . 9))
  ((map (1 (and x (pred (> 10))))) x)
  (_ 'not-found))

gives not-found, not 9 (which would also make sense).


Thanks,

Michael.



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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-07 18:36         ` Michael Heerdegen
@ 2015-06-08 14:16           ` Stefan Monnier
  2015-06-08 15:34             ` Michael Heerdegen
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2015-06-08 14:16 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Nicolas Petton, emacs-devel

> Perhaps we should also say that only one occurrence of KEY is tested in
> the alist case (is that true?), e.g.

The usual semantics of alists is that a new element completely hides
earlier elements for the same key.  Otherwise, it's not a "map" any
more, but a general relation.


        Stefan



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

* Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
  2015-06-08 14:16           ` Stefan Monnier
@ 2015-06-08 15:34             ` Michael Heerdegen
  0 siblings, 0 replies; 7+ messages in thread
From: Michael Heerdegen @ 2015-06-08 15:34 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@IRO.UMontreal.CA> writes:

> > Perhaps we should also say that only one occurrence of KEY is tested in
> > the alist case (is that true?), e.g.
>
> The usual semantics of alists is that a new element completely hides
> earlier elements for the same key.  Otherwise, it's not a "map" any
> more, but a general relation.

Ok, makes sense.


Michael.




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

end of thread, other threads:[~2015-06-08 15:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20150602201956.22733.21800@vcs.savannah.gnu.org>
     [not found] ` <E1YzsfE-0005vH-4d@vcs.savannah.gnu.org>
2015-06-03  1:06   ` [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it Stefan Monnier
2015-06-04 20:18     ` Nicolas Petton
2015-06-04 22:39       ` Stefan Monnier
2015-06-05  8:09         ` Nicolas Petton
2015-06-07 18:36         ` Michael Heerdegen
2015-06-08 14:16           ` Stefan Monnier
2015-06-08 15:34             ` Michael Heerdegen

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