From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.devel 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 Message-ID: <87fu61ygg5.fsf@ericabrahamsen.net> References: <20180215203406.64372-1-phst@google.com>> <2ce9b5db-b17d-415c-8dc6-6046a31269be@default> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1518737688 12085 195.159.176.226 (15 Feb 2018 23:34:48 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 15 Feb 2018 23:34:48 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: Philipp Stephani , Philipp Stephani , emacs-devel@gnu.org To: Drew Adams Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Feb 16 00:34:43 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1emT2h-0001Gr-Jd for ged-emacs-devel@m.gmane.org; Fri, 16 Feb 2018 00:34:19 +0100 Original-Received: from localhost ([::1]:43902 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emT4j-0004Ti-FJ for ged-emacs-devel@m.gmane.org; Thu, 15 Feb 2018 18:36:25 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51575) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emT3u-0004RS-Fy for emacs-devel@gnu.org; Thu, 15 Feb 2018 18:35:35 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emT3r-0001ut-7r for emacs-devel@gnu.org; Thu, 15 Feb 2018 18:35:34 -0500 Original-Received: from mail.ericabrahamsen.net ([50.56.99.223]:49120) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1emT3r-0001t0-2R for emacs-devel@gnu.org; Thu, 15 Feb 2018 18:35:31 -0500 Original-Received: from localhost (71-35-176-243.tukw.qwest.net [71.35.176.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) (Authenticated sender: eric@ericabrahamsen.net) by mail.ericabrahamsen.net (Postfix) with ESMTPSA id BF70CBE89A; Thu, 15 Feb 2018 23:35:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ericabrahamsen.net; s=mail; t=1518737723; bh=tyNEmrRev1TN5TPrRWT5wsCdt3ROes0SoMPnoBlov9o=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=XRYyXQpapWc+IMsOQDBRsE2X6NHsHKpObeYYSi0k6cK3Sbz6aIqWo9UdMAUAIbxT6 Xq06RPOdsa2QEMYc7DZn3S6No4Jn9BqnMe/++LCV2FJUJ61/ybngBFRcS/nVkga5W6 G9hUBCqu8QbxSyQ5aAd4unNIB6DhPFyR79wxDKvM= In-Reply-To: <2ce9b5db-b17d-415c-8dc6-6046a31269be@default> (Drew Adams's message of "Thu, 15 Feb 2018 14:08:42 -0800 (PST)") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 50.56.99.223 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:222789 Archived-At: Drew Adams 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" ) (gethash "key"
)) 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