28724d30efef

Merge.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 30 Apr 2019 17:06:46 -0400
parents 861b0bbcb319 (diff) 71b34a9a3413 (current diff)
children e48985ef1aac
branches/tags (none)
files package.lisp

Changes

--- a/package.lisp	Fri Mar 08 22:09:23 2019 -0500
+++ b/package.lisp	Tue Apr 30 17:06:46 2019 -0400
@@ -233,7 +233,13 @@
   (:documentation "Utilities for operating on hash tables.")
   (:export
     :hash-table-contents
-    :mutate-hash-values))
+    :mutate-hash-values
+    :remhash-if
+    :remhash-if-not
+    :remhash-if-key
+    :remhash-if-not-key
+    :remhash-if-value
+    :remhash-if-not-value))
 
 
 (defpackage :losh.random
--- a/src/hash-tables.lisp	Fri Mar 08 22:09:23 2019 -0500
+++ b/src/hash-tables.lisp	Tue Apr 30 17:06:46 2019 -0400
@@ -15,3 +15,64 @@
   "Return a fresh list of `(key value)` elements of `hash-table`."
   (gathering (maphash (compose #'gather #'list) hash-table)))
 
+(defun remhash-if (test hash-table)
+  "Remove elements which satisfy `(test key value)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (when (funcall test k v)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)
+
+(defun remhash-if-not (test hash-table)
+  "Remove elements which don't satisfy `(test key value)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (unless (funcall test k v)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)
+
+(defun remhash-if-key (test hash-table)
+  "Remove elements which satisfy `(test key)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (declare (ignore v))
+             (when (funcall test k)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)
+
+(defun remhash-if-not-key (test hash-table)
+  "Remove elements which satisfy don't `(test key)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (declare (ignore v))
+             (unless (funcall test k)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)
+
+(defun remhash-if-value (test hash-table)
+  "Remove elements which satisfy `(test value)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (when (funcall test v)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)
+
+(defun remhash-if-not-value (test hash-table)
+  "Remove elements which satisfy don't `(test value)` from `hash-table`.
+
+  Returns the hash table."
+  (maphash (lambda (k v)
+             (unless (funcall test v)
+               (remhash k hash-table)))
+           hash-table)
+  hash-table)