Skip to content

Instantly share code, notes, and snippets.

@pnathan
Last active December 12, 2015 03:48
Show Gist options
  • Save pnathan/4709844 to your computer and use it in GitHub Desktop.
Save pnathan/4709844 to your computer and use it in GitHub Desktop.
map constructor idea, mooched from rdtsc
(defun make-map (pairs
&key
(test #'eql)
(default nil))
"Creates a hash table with the list pairs; the CAR of each element
in the pairs is the key, the CDR is the value.
Hash table tests are :test
The default value of elements is `default`"
(let ((new-hash (make-hash-table :test test)))
(loop for (key . value) in pairs do
(setf (gethash key new-hash default) (car value)))
new-hash))
; CL-USER> (make-map '((1 2) (10 100) (30 "aaa")))
; #<HASH-TABLE :TEST EQL :COUNT 3 {1006A06183}>
; CL-USER> (print-hash-table-1 (make-map '((1 2) (10 100) (30 "aaa")))) ;;print-hash-table-1 is a function I wrote
; 30 => aaa
; 10 => 100
; 1 => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment