define-module for elisp special modules
[bpt/guile.git] / module / ice-9 / hash-table.scm
CommitLineData
5063f0a9
DT
1;;;; hash-table.scm --- Additional hash table procedures
2;;;; Copyright (C) 2013 Free Software Foundation, Inc.
3;;;;
4;;;; This library is free software; you can redistribute it and/or
5;;;; modify it under the terms of the GNU Lesser General Public
6;;;; License as published by the Free Software Foundation; either
7;;;; version 3 of the License, or (at your option) any later version.
8;;;;
9;;;; This library is distributed in the hope that it will be useful,
10;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12;;;; Lesser General Public License for more details.
13;;;;
14;;;; You should have received a copy of the GNU Lesser General Public
15;;;; License along with this library; if not, write to the Free Software
16;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17;;;;
18
19(define-module (ice-9 hash-table)
20 #:export (alist->hash-table
21 alist->hashq-table
22 alist->hashv-table
23 alist->hashx-table))
24
25(define-syntax-rule (define-alist-converter name hash-set-proc)
26 (define (name alist)
27 "Convert ALIST into a hash table."
28 (let ((table (make-hash-table)))
29 (for-each (lambda (pair)
30 (hash-set-proc table (car pair) (cdr pair)))
31 (reverse alist))
32 table)))
33
34(define-alist-converter alist->hash-table hash-set!)
35(define-alist-converter alist->hashq-table hashq-set!)
36(define-alist-converter alist->hashv-table hashv-set!)
37
38(define (alist->hashx-table hash assoc alist)
39 "Convert ALIST into a hash table with custom HASH and ASSOC
40procedures."
41 (let ((table (make-hash-table)))
42 (for-each (lambda (pair)
43 (hashx-set! hash assoc table (car pair) (cdr pair)))
44 (reverse alist))
45 table))