hash-util.scm 781 B

123456789101112131415161718192021222324252627
  1. (define-module (data-mining hash-util)
  2. #:export (hash-table-size
  3. hash-move-key
  4. hash-subset))
  5. (define (hash-table-size table)
  6. "Return the number of association in the hash table."
  7. (hash-count (const #t) table))
  8. (define* (hash-move-key table oldkey newkey #:optional (default #f))
  9. "Modify TABLE such that the value pointed to by OLDKEY is pointed to
  10. by NEWKEY instead."
  11. (let ((val (hash-ref table oldkey default)))
  12. (hash-remove! table oldkey)
  13. (hash-set! table newkey val)))
  14. (define (hash-subset table keys)
  15. "Return a new hash table which contains the keys KEYS from TABLE,
  16. mapped to the same values."
  17. (let ((table* (make-hash-table)))
  18. (for-each
  19. (lambda (key)
  20. (hash-set! table* key
  21. (hash-ref table key)))
  22. keys)
  23. table*))