From: Philipp Stephani <p.stephani2@gmail.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: [PATCH] Add new function to test whether a key is present in a hash table.
Date: Thu, 15 Feb 2018 21:34:06 +0100 [thread overview]
Message-ID: <20180215203406.64372-1-phst@google.com> (raw)
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.
* src/fns.c (Fhash_table_contains_p): New function.
* test/src/fns-tests.el (hash-table-contains-p): New unit test.
* doc/lispref/hash.texi (Hash Access): Document new function.
---
doc/lispref/hash.texi | 12 ++++++++++++
etc/NEWS | 3 +++
src/fns.c | 10 ++++++++++
test/src/fns-tests.el | 8 ++++++++
4 files changed, 33 insertions(+)
diff --git a/doc/lispref/hash.texi b/doc/lispref/hash.texi
index ddd46a55ed..8848f0462e 100644
--- a/doc/lispref/hash.texi
+++ b/doc/lispref/hash.texi
@@ -195,6 +195,18 @@ Hash Access
association in @var{table}.
@end defun
+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.
+@end defun
+
@defun puthash key value table
This function enters an association for @var{key} in @var{table}, with
value @var{value}. If @var{key} already has an association in
diff --git a/etc/NEWS b/etc/NEWS
index 71569c95ad..5c9363c052 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -282,6 +282,9 @@ depending on the new customizable variable 'read-answer-short'.
Specifically, it puts the module name into 'load-history', prints
loading messages if requested, and protects against recursive loads.
+** New function 'hash-table-contains-p' to check whether a hash table
+contains a certain key.
+
\f
* Changes in Emacs 27.1 on Non-Free Operating Systems
diff --git a/src/fns.c b/src/fns.c
index 47457e44c8..6075544c69 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4650,6 +4650,15 @@ DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0,
}
+DEFUN ("hash-table-contains-p", Fhash_table_contains_p, Shash_table_contains_p,
+ 2, 2, NULL,
+ doc: /* Return t if TABLE contains KEY, nil otherwise. */)
+ (Lisp_Object key, Lisp_Object table)
+{
+ return hash_lookup (check_hash_table (table), key, NULL) >= 0 ? Qt : Qnil;
+}
+
+
DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0,
doc: /* Look up KEY in TABLE and return its associated value.
If KEY is not found, return DFLT which defaults to nil. */)
@@ -5145,6 +5154,7 @@ syms_of_fns (void)
defsubr (&Shash_table_weakness);
defsubr (&Shash_table_p);
defsubr (&Sclrhash);
+ defsubr (&Shash_table_contains_p);
defsubr (&Sgethash);
defsubr (&Sputhash);
defsubr (&Sremhash);
diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el
index f8554636ba..f651ee8985 100644
--- a/test/src/fns-tests.el
+++ b/test/src/fns-tests.el
@@ -575,4 +575,12 @@ dot2
:type 'wrong-type-argument)
'(wrong-type-argument plistp (:foo 1 . :bar)))))
+(ert-deftest hash-table-contains-p ()
+ (let ((h (make-hash-table)))
+ (puthash 1.5 'foo h)
+ (puthash 2.5 nil h)
+ (should (equal (hash-table-contains-p 1.5 h) t))
+ (should (equal (hash-table-contains-p 2.5 h) t))
+ (should (equal (hash-table-contains-p 2 h) nil))))
+
(provide 'fns-tests)
--
2.16.1
next reply other threads:[~2018-02-15 20:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-15 20:34 Philipp Stephani [this message]
2018-02-15 22:03 ` [PATCH] Add new function to test whether a key is present in a hash table 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
[not found] <<20180215203406.64372-1-phst@google.com>
2018-02-15 22:08 ` Drew Adams
2018-02-15 23:35 ` Eric Abrahamsen
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
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=20180215203406.64372-1-phst@google.com \
--to=p.stephani2@gmail.com \
--cc=emacs-devel@gnu.org \
--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 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).