• person rss_feed

    Michał "phoe" Herda’s feed

    Blog

    • chevron_right

      PRINT-HASH-TABLE-READABLY

      Michał "phoe" Herda · Sunday, 23 December, 2018 - 11:53

    (This is a repost of an old blog post of mine from Teknik.)

    #alexandria #common-lisp

    I had a hash table in a running Common Lisp image that I wanted to print readably while preserving object identity (mostly to avoid data duplication).

    Alexandria and Bike from #lisp came to the rescue.

    ;;; (ql:quickload :alexandria)
    
    (defun print-hash-table-readably (hash-table
                                      &optional (stream *standard-output*))
      "Prints a hash table readably using ALEXANDRIA:ALIST-HASH-TABLE."
      (let ((test (hash-table-test hash-table))
            (*print-circle* t)
            (*print-readably* t))
        (format stream "#.(ALEXANDRIA:ALIST-HASH-TABLE '(~%")
        (maphash (lambda (k v) (format stream "   (~S . ~S)~%" k v)) hash-table)
        (format stream "   ) :TEST '~A)" test)
        hash-table))