Common Lisp: shorthand to initialize a hash table with many entries

I'm looking for a possibly non-verbose portable way to initialize a hash table in Common Lisp. E.g. something that works for constant hash tables, but also to pre-load variable hashes. In CLISP I am using:

(defconstant +my-map+ #S(HASH-TABLE :TEST FASTHASH-EQ
  (key1 . "value1")
  ...
  (keyN . "valueN")
))

but unfortunately this format only works on CLISP.

Upvotes: 10

Views: 3041

Answers (4)

CL-USER
CL-USER

Reputation: 786

Wow, a question from 9 years ago getting another answer. Coincidentally make-hash came out 2 months after this question was asked and is generally useful for this kind of thing.

Upvotes: 0

Ehvince
Ehvince

Reputation: 18415

The Serapeum library has dict:

(dict :a 1 :b 2 :c 3)
#<HASH-TABLE :TEST EQUAL :COUNT 3 {1008906D13}>

You can pretty print hash tables:

CL-USER> (toggle-pretty-print-hash-table)
T
CL-USER> (dict :a 1 :b 2 :c 3)
(dict
  :A 1
  :B 2
  :C 3
 ) 

which is a representation that can be read back in.

We can also use pretty-print-hash-table directly.

Serapeum is a high quality library.

ps: my CIEL meta-package makes dict available by default.

Upvotes: 1

Rainer Joswig
Rainer Joswig

Reputation: 139411

One can programmatically construct a hash table at read time:

(defvar *ht* #.(let ((ht (make-hash-table)))
                 (loop for (key . value) in
                       '((a . 1) (b . 2) (c . 3))
                       do (setf (gethash key ht) value))
                 ht))

(describe *ht*)

#. is used for read time evaluation. The compiler then will dump the hash table to the FASL file.

This can then be compiled:

Using SBCL:

* (compile-file "/tmp/test.lisp")

; compiling file "/private/tmp/test.lisp" (written 24 MAY 2012 10:08:49 PM):
; compiling (DEFVAR *HT* ...)
; compiling (DESCRIBE *HT*)

; /tmp/test.fasl written
; compilation finished in 0:00:00.360
#P"/private/tmp/test.fasl"
NIL
NIL
* (load *)

#<HASH-TABLE :TEST EQL :COUNT 3 {100299EA43}>
  [hash-table]

Occupancy: 0.2
Rehash-threshold: 1.0
Rehash-size: 1.5
Size: 16
Synchronized: no
T
* *ht*

#<HASH-TABLE :TEST EQL :COUNT 3 {100299EA43}>

Creating a hash table as a function:

(defun create-hashtable (alist
                         &key (test 'eql)
                         &aux (ht (make-hash-table :test test)))
  (loop for (key . value) in alist
        do (setf (gethash key ht) value))
  ht)

Upvotes: 9

Miron Brezuleanu
Miron Brezuleanu

Reputation: 3060

Alexandria has the alist-hash-table function, which you may find useful.

Upvotes: 2

Related Questions