adapt print-hash-table for dict

This commit is contained in:
vindarel 2020-11-02 15:33:10 +01:00
parent 6f42cd4835
commit 435ce2ec5a
2 changed files with 20 additions and 16 deletions

View file

@ -213,22 +213,25 @@ Here's how we can create a hash-table with keys and values:
;; create a hash-table:
(dict :a 1 :b 2 :c 3)
;; =>
#{EQUAL
:A 1
:B 2
:C 3
}
(dict
:A 1
:B 2
:C 3
)
#+end_src
As seen above, hash-tables are pretty-printed by default. In default
Common Lisp, they are printed like so:
In default Common Lisp, you would do:
#<HASH-TABLE :TEST EQUAL :COUNT 3 {1006CE5613}>
#+BEGIN_SRC lisp
(let ((ht (make-hash-table :test 'equal)))
(setf (gethash :a ht) 1)
(setf (gethash :b ht) 2)
(setf (gethash :c ht) 3)
ht)
;; #<HASH-TABLE :TEST EQUAL :COUNT 3 {1006CE5613}>
#+end_src
However, our represetation has an important drawback: we (currently?) can't read
the hash-table literal back in.
# although rutils does it with the #h notation, which we would like to avoid in favour of dict.
As seen above, hash-tables are pretty-printed by default.
You can toggle the representation with =toggle-print-hash-table=, or
by setting

View file

@ -197,8 +197,9 @@
(pprint-logical-block (stream nil)
(format stream "~&")
(format stream "~vt" *current-pprint-indentation*)
(princ "#{" stream)
(unless (eq (hash-table-test ht) 'eql)
(princ "(dict " stream)
(unless (eq (hash-table-test ht) 'equal)
(princ #\' stream)
(princ (hash-table-test ht) stream))
(incf *current-pprint-indentation*)
(format stream "~vt" *current-pprint-indentation*)
@ -223,12 +224,12 @@
(decf *current-pprint-indentation*)
(format stream "~vt" *current-pprint-indentation*)
(format stream "~&")
(format stream "~vt} " *current-pprint-indentation*)))
(format stream "~vt) " *current-pprint-indentation*)))
ht)
;; ;; Or:
;; (defmethod print-hash-table ((object hash-table) stream)
;; ;; XXX: we can not read this back.
;; ;; we don't use rutils's pretty printing because we don't want the #h notaton
;; (format stream "#HASH{~a, ~{~{~a: ~a~}~^, ~}}"
;; (hash-table-test object)
;; (loop for key being the hash-keys of object