Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Readable hash tables
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ivar Rummelhoff  
View profile  
 More options Jun 14 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Ivar Rummelhoff <iva...@math.uio.no>
Date: 2000/06/14
Subject: Re: Readable hash tables

>>>>> Erik Naggum <e...@naggum.no> :

> Take a look at *print-pprint-dispatch* and section 22.2.1.4 in CLtS.

Just what I was looking for!  (as always)

For those who might be interested; this is what I came up with:

(defun hash-table-to-alist (hash-table)
  (loop for key being the hash-keys of hash-table
      using (hash-value value)
      collect (cons key value)))

(defun hash-print (s h)
  (write-string "#h" s)
  (write (cons (hash-table-test h)
               (hash-table-to-alist h))
         :stream s
         :pretty t))

(defun hash-read (stream subchar arg)
  (declare (ignore subchar arg))
  (destructuring-bind (test &rest entries) (read stream t nil t)
    (let ((tab (make-hash-table :test test)))
      (loop for (key . value) in entries
          do (setf (gethash key tab) value))
      tab)))

(let ((pprint-dispatch
       (let ((tab (copy-pprint-dispatch nil)))
         (set-pprint-dispatch 'hash-table #'hash-print 0 tab)
         tab))
      (write-dispatch
       (let ((tab (copy-pprint-dispatch nil)))
         (set-pprint-dispatch
          t #'(lambda (s obj) (write obj :stream s :pretty nil))
          0 tab)
         (set-pprint-dispatch 'hash-table #'hash-print 1 tab)
         tab)))
  (defun mi-write (obj &rest args)
    (let ((*print-pprint-dispatch*
           (if (getf args :pretty *print-pretty*) pprint-dispatch
             (progn (setf (getf args :pretty) t) write-dispatch))))
      (apply #'write obj args))))

(let ((readtable
       (let ((tab (copy-readtable nil)))
         (set-dispatch-macro-character #\# #\h #'hash-read tab)
         tab)))
  (defun mi-read (&rest args)
    (let ((*readtable* readtable))
      (apply #'read args))))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.