-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashtable-utilities.lisp
42 lines (35 loc) · 1.04 KB
/
hashtable-utilities.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(defpackage :hashtable-utilities
(:nicknames :hash-table-utilities :hashtable-utils :ht-utils)
(:use :cl))
(in-package :ht-utils)
(defun print-hashtable (hashtable &optional (stream t))
(format stream "{")
(with-hash-table-iterator (pair hashtable)
(loop
for (any-left k v) = (multiple-value-list (pair))
while any-left
do (format stream " ~A: ~A " k v)
finally (format stream "}"))))
(defmethod print-object ((dict hash-table) stream)
"method to print a hashtable in a JSON style"
(print-hashtable dict stream))
(defun hashtable-values (hashtable)
(let ((val Nil))
(progn
(maphash (lambda (k v)
(declare (ignore k))
(push v val)) hashtable)
(reverse val))))
(defun hashtable-keys (hashtable)
(let ((keys Nil))
(progn
(maphash (lambda (k v)
(declare (ignore v))
(push k keys)) hashtable)
(reverse keys))))
(defun hashtable-pairs (hashtable)
(let ((pairs Nil))
(progn
(maphash (lambda (k v)
(push (list k v) pairs)) hashtable)
(reverse pairs))))