From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Thompson Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Add procedures to convert alists into hash tables Date: Mon, 18 Nov 2013 21:00:58 -0500 Message-ID: <528AC65A.2080509@worcester.edu> References: <5263E55A.6040307@worcester.edu> <87txg0p805.fsf@gnu.org> <87iow6pqox.fsf@netris.org> <87mwlhlmgr.fsf@gnu.org> <5289726B.6030302@worcester.edu> <52897E7B.6000602@worcester.edu> <8738mtmox6.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------020907050504010802050206" X-Trace: ger.gmane.org 1384826471 2459 80.91.229.3 (19 Nov 2013 02:01:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 19 Nov 2013 02:01:11 +0000 (UTC) Cc: Mark H Weaver , guile-devel To: =?UTF-8?B?THVkb3ZpYyBDb3VydMOocw==?= Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Tue Nov 19 03:01:15 2013 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Viacr-0002el-GS for guile-devel@m.gmane.org; Tue, 19 Nov 2013 03:01:13 +0100 Original-Received: from localhost ([::1]:46689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Viacr-0003nT-1c for guile-devel@m.gmane.org; Mon, 18 Nov 2013 21:01:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41253) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Viaci-0003nC-31 for guile-devel@gnu.org; Mon, 18 Nov 2013 21:01:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Viacb-0001fi-Jr for guile-devel@gnu.org; Mon, 18 Nov 2013 21:01:04 -0500 Original-Received: from na3sys009aog116.obsmtp.com ([74.125.149.240]:56405) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Viacb-0001fP-9b for guile-devel@gnu.org; Mon, 18 Nov 2013 21:00:57 -0500 Original-Received: from mail-qe0-f41.google.com ([209.85.128.41]) (using TLSv1) by na3sys009aob116.postini.com ([74.125.148.12]) with SMTP ID DSNKUorGVzeY5/n9grde5QzRorop5eRudCLg@postini.com; Mon, 18 Nov 2013 18:00:56 PST Original-Received: by mail-qe0-f41.google.com with SMTP id x7so4747135qeu.28 for ; Mon, 18 Nov 2013 18:00:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=8+190VXtB1L4VwOcNGeOAE7J/8agHtH4mR9ljynVn8k=; b=H+/n1gOmmKWw1kOZ1fYK4nog1iuHtxO4rA+LV9AXKMz2f/ZGvnuNk5zYKrgEaLv8OS Hr7KWb87JqqHxdsn29s6fi93CpvGhtZrZqVubHI54LSkKPu1FxQhxXJ9VCw4rjPNV6Hm M8Coqwe9OcVXcnkdXd3uDhhETMQ8YK6p0JDfS4/YHIGPRdMpl1WRFkAWN5X9HZFH1sne hmAU2T2kj5DjnLrpw14fj2IAA2N0ckOd8QWGKbfuv66cWM+47r6p/Ls5jE6MYEVR9KWB 4pHq0BhRQeOhyWxDjI2GezQas2KjQKvk6b20zedygtx9lphn3JNuzaE9WgvVRh3dCjKt HkdA== X-Gm-Message-State: ALoCoQl6UjsOqXv5OBMJtAHDXBynt/KEkoWU38QVXDeHPHxa4bR51x9f2HxGiNfSfxw62T+RSELhPWw9OtKBF/TrbzZRo+KgxIyc9oe1XS/Yify7rcw12hMMU+3tfiO6jFC4CM5P/mriX1Hf4/X67fqRo6hY9tNnZA== X-Received: by 10.49.117.41 with SMTP id kb9mr38657966qeb.79.1384826454470; Mon, 18 Nov 2013 18:00:54 -0800 (PST) X-Received: by 10.49.117.41 with SMTP id kb9mr38657957qeb.79.1384826454378; Mon, 18 Nov 2013 18:00:54 -0800 (PST) Original-Received: from [192.168.1.157] (209-6-40-86.c3-0.smr-ubr1.sbo-smr.ma.cable.rcn.com. [209.6.40.86]) by mx.google.com with ESMTPSA id h9sm46598248qaq.9.2013.11.18.18.00.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 18 Nov 2013 18:00:52 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131005 Icedove/17.0.9 In-Reply-To: <8738mtmox6.fsf@gnu.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 74.125.149.240 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:16755 Archived-At: This is a multi-part message in MIME format. --------------020907050504010802050206 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 11/18/2013 03:32 PM, Ludovic Courtès wrote: > David Thompson skribis: > >> From 03f604cc3dfffb816cfe66a355e36ede337749f1 Mon Sep 17 00:00:00 2001 >> From: David Thompson >> Date: Sat, 19 Oct 2013 22:43:37 -0400 >> Subject: [PATCH] Add procedures to convert alists into hash tables. >> >> * module/ice-9/hash-table.scm: New module. >> >> * test-suite/tests/hash.test ("alist conversion"): Add tests. >> >> * doc/ref/api-compound.texi (Hash Table Reference): Add docs. > > Agreed with Mark’s comment, but otherwise looks good to me. > >> +(define-syntax-rule (define-alist-converter name hash-set-proc) >> + (define (name alist) > > [...] > >> +(define (alist->hashx-table hash assoc alist) >> + "Convert @var{alist} into a hash table with custom @var{hash} and >> +@var{assoc} procedures." > > Currently Texinfo markup in docstrings is left uninterpreted so it > can/should be avoided (it may change in the future, but the future’s not > now ;-)). > > Thanks! > > Ludo’. > Fixed texi docs and docstrings. - Dave --------------020907050504010802050206 Content-Type: text/x-patch; name="0001-Add-procedures-to-convert-alists-into-hash-tables.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Add-procedures-to-convert-alists-into-hash-tables.patch" >From 37571c1a11d53cee176bf93ddd539f0dd91f3669 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 19 Oct 2013 22:43:37 -0400 Subject: [PATCH] Add procedures to convert alists into hash tables. * module/ice-9/hash-table.scm: New module. * test-suite/tests/hash.test ("alist conversion"): Add tests. * doc/ref/api-compound.texi (Hash Table Reference): Add docs. --- doc/ref/api-compound.texi | 21 +++++++++++++++++++++ module/Makefile.am | 1 + module/ice-9/hash-table.scm | 45 +++++++++++++++++++++++++++++++++++++++++++++ test-suite/tests/hash.test | 38 +++++++++++++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 module/ice-9/hash-table.scm diff --git a/doc/ref/api-compound.texi b/doc/ref/api-compound.texi index 94e0145..0b14c48 100644 --- a/doc/ref/api-compound.texi +++ b/doc/ref/api-compound.texi @@ -3829,6 +3829,27 @@ then it can use @var{size} to avoid rehashing when initial entries are added. @end deffn +@deffn {Scheme Procedure} alist->hash-table alist +@deffnx {Scheme Procedure} alist->hashq-table alist +@deffnx {Scheme Procedure} alist->hashv-table alist +@deffnx {Scheme Procedure} alist->hashx-table hash assoc alist +Convert @var{alist} into a hash table. When keys are repeated in +@var{alist}, the leftmost association takes precedence. + +@example +(use-modules (ice-9 hash-table)) +(alist->hash-table '((foo . 1) (bar . 2))) +@end example + +When converting to an extended hash table, custom @var{hash} and +@var{assoc} procedures must be provided. + +@example +(alist->hashx-table hash assoc '((foo . 1) (bar . 2))) +@end example + +@end deffn + @deffn {Scheme Procedure} hash-table? obj @deffnx {C Function} scm_hash_table_p (obj) Return @code{#t} if @var{obj} is a abstract hash table object. diff --git a/module/Makefile.am b/module/Makefile.am index e8dcd4a..8a7befd 100644 --- a/module/Makefile.am +++ b/module/Makefile.am @@ -207,6 +207,7 @@ ICE_9_SOURCES = \ ice-9/format.scm \ ice-9/futures.scm \ ice-9/getopt-long.scm \ + ice-9/hash-table.scm \ ice-9/hcons.scm \ ice-9/i18n.scm \ ice-9/iconv.scm \ diff --git a/module/ice-9/hash-table.scm b/module/ice-9/hash-table.scm new file mode 100644 index 0000000..ca9d2fd --- /dev/null +++ b/module/ice-9/hash-table.scm @@ -0,0 +1,45 @@ +;;;; hash-table.scm --- Additional hash table procedures +;;;; Copyright (C) 2013 Free Software Foundation, Inc. +;;;; +;;;; This library is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU Lesser General Public +;;;; License as published by the Free Software Foundation; either +;;;; version 3 of the License, or (at your option) any later version. +;;;; +;;;; This library is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;;; Lesser General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU Lesser General Public +;;;; License along with this library; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +;;;; + +(define-module (ice-9 hash-table) + #:export (alist->hash-table + alist->hashq-table + alist->hashv-table + alist->hashx-table)) + +(define-syntax-rule (define-alist-converter name hash-set-proc) + (define (name alist) + "Convert ALIST into a hash table." + (let ((table (make-hash-table))) + (for-each (lambda (pair) + (hash-set-proc table (car pair) (cdr pair))) + (reverse alist)) + table))) + +(define-alist-converter alist->hash-table hash-set!) +(define-alist-converter alist->hashq-table hashq-set!) +(define-alist-converter alist->hashv-table hashv-set!) + +(define (alist->hashx-table hash assoc alist) + "Convert ALIST into a hash table with custom HASH and ASSOC +procedures." + (let ((table (make-hash-table))) + (for-each (lambda (pair) + (hashx-set! hash assoc table (car pair) (cdr pair))) + (reverse alist)) + table)) diff --git a/test-suite/tests/hash.test b/test-suite/tests/hash.test index 3bd4004..ad247f5 100644 --- a/test-suite/tests/hash.test +++ b/test-suite/tests/hash.test @@ -18,7 +18,8 @@ (define-module (test-suite test-numbers) #:use-module (test-suite lib) - #:use-module (ice-9 documentation)) + #:use-module (ice-9 documentation) + #:use-module (ice-9 hash-table)) ;;; ;;; hash @@ -81,6 +82,41 @@ (write (make-hash-table 100))))))) ;;; +;;; alist->hash-table +;;; + +(with-test-prefix + "alist conversion" + + (pass-if "alist->hash-table" + (let ((table (alist->hash-table '(("foo" . 1) + ("bar" . 2) + ("foo" . 3))))) + (and (= (hash-ref table "foo") 1) + (= (hash-ref table "bar") 2)))) + + (pass-if "alist->hashq-table" + (let ((table (alist->hashq-table '((foo . 1) + (bar . 2) + (foo . 3))))) + (and (= (hashq-ref table 'foo) 1) + (= (hashq-ref table 'bar) 2)))) + + (pass-if "alist->hashv-table" + (let ((table (alist->hashv-table '((1 . 1) + (2 . 2) + (1 . 3))))) + (and (= (hashv-ref table 1) 1) + (= (hashv-ref table 2) 2)))) + + (pass-if "alist->hashx-table" + (let ((table (alist->hashx-table hash assoc '((foo . 1) + (bar . 2) + (foo . 3))))) + (and (= (hashx-ref hash assoc table 'foo) 1) + (= (hashx-ref hash assoc table 'bar) 2))))) + +;;; ;;; usual set and reference ;;; -- 1.8.4.2 --------------020907050504010802050206--