hash-table.scm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. (define-module (ice-9 hash-table)
  19. #:export (alist->hash-table
  20. alist->hashq-table
  21. alist->hashv-table
  22. alist->hashx-table))
  23. (define-syntax-rule (define-alist-converter name hash-set-proc)
  24. (define (name alist)
  25. "Convert ALIST into a hash table."
  26. (let ((table (make-hash-table)))
  27. (for-each (lambda (pair)
  28. (hash-set-proc table (car pair) (cdr pair)))
  29. (reverse alist))
  30. table)))
  31. (define-alist-converter alist->hash-table hash-set!)
  32. (define-alist-converter alist->hashq-table hashq-set!)
  33. (define-alist-converter alist->hashv-table hashv-set!)
  34. (define (alist->hashx-table hash assoc alist)
  35. "Convert ALIST into a hash table with custom HASH and ASSOC
  36. procedures."
  37. (let ((table (make-hash-table)))
  38. (for-each (lambda (pair)
  39. (hashx-set! hash assoc table (car pair) (cdr pair)))
  40. (reverse alist))
  41. table))