From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: Drew Adams <drew.adams@oracle.com>
Cc: Philipp Stephani <phst@google.com>,
Philipp Stephani <p.stephani2@gmail.com>,
emacs-devel@gnu.org
Subject: Re: [PATCH] Add new function to test whether a key is present in a hash table.
Date: Thu, 15 Feb 2018 15:35:22 -0800 [thread overview]
Message-ID: <87fu61ygg5.fsf@ericabrahamsen.net> (raw)
In-Reply-To: <2ce9b5db-b17d-415c-8dc6-6046a31269be@default> (Drew Adams's message of "Thu, 15 Feb 2018 14:08:42 -0800 (PST)")
Drew Adams <drew.adams@oracle.com> writes:
>> Such a function is useful because in Emacs Lisp, 'gethash' cannot
>> return whether the key is present as in Common Lisp, and using
>> 'gethash' alone to test for presence is nontrivial.
>
> Glad to see this. Everyone was coding their own (in Lisp).
+1
>> +While it might be tempting to use @code{gethash} to check whether a
>> +key is present in a hash table, keep in mind that you often need to
>> +distinguish between @var{key} being absent and @var{key} being mapped
>> +to @var{default}. To easily distinguish between these two cases,
>> +there's another function to explicitly check whether a key is present:
>> +
>> +@defun hash-table-contains-p key table
>> +This function looks up @var{key} in @var{table}; it returns @code{t}
>> +if @var{key} is present, and @code{nil} otherwise. The associated
>> +value is ignored.
>
> 1. I don't think we need to say "The associated value is
> ignored". Nothing suggests that it would be used in any way.
>
> 2. The function name should have `key' in it, I think. Suggestion:
> `hash-table-contains-key-p' or even just `hash-table-key-p'.
>
> 3. Another possibility could be to return, as the true value,
> a cons of the value associated with the key. IOW, instead
> of returning `t' we would return `(THE-VALUE)'. If this
> is not particularly costly it might be a bit more useful.
I would really like to see a function that didn't require us to:
(and (hash-table-key-p "key" <table>) (gethash "key" <table>))
Ie, an all-in-one function (happy to see it implemented in elisp, a la
Stefan's solution) that gives us both pieces of information at once.
Drew's cons-cell seems like a good middle ground. We could even call it
`hash-table-get', to contrast with `gethash', and still have it accept
(and return) the optional DFLT argument.
Eric
next prev parent reply other threads:[~2018-02-15 23:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <<20180215203406.64372-1-phst@google.com>
2018-02-15 22:08 ` [PATCH] Add new function to test whether a key is present in a hash table Drew Adams
2018-02-15 23:35 ` Eric Abrahamsen [this message]
2018-02-16 6:51 ` John Wiegley
2018-02-18 14:29 ` Richard Copley
2018-02-19 6:02 ` John Wiegley
2018-02-19 17:26 ` Drew Adams
2018-02-15 20:34 Philipp Stephani
2018-02-15 22:03 ` Clément Pit-Claudel
2019-04-19 17:33 ` Philipp Stephani
2018-02-15 22:59 ` Stefan Monnier
2018-02-16 1:03 ` Drew Adams
2018-02-16 1:59 ` Stefan Monnier
2018-02-16 2:50 ` Drew Adams
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fu61ygg5.fsf@ericabrahamsen.net \
--to=eric@ericabrahamsen.net \
--cc=drew.adams@oracle.com \
--cc=emacs-devel@gnu.org \
--cc=p.stephani2@gmail.com \
--cc=phst@google.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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.