Skip to content

Commit

Permalink
hashtable-utilities.lisp added
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPyser committed Jul 8, 2015
1 parent 9ae7dbf commit 8494636
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions hashtable-utilities.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(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))))

0 comments on commit 8494636

Please sign in to comment.