emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let


From: Stefan Monnier
Subject: Re: [Emacs-diffs] map 988d721: Add a pcase pattern for maps and `map-let' based on it
Date: Tue, 02 Jun 2015 21:06:44 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]