# HG changeset patch # User Steve Losh # Date 1557070896 14400 # Node ID e48985ef1aac7a0190cb6abfc416d979dd4b9e36 # Parent 5e42deadf7733c68b3b418ec69fec71281433f04# Parent 28724d30efef19dacec09f446d6b9a2b76ec8f85 Merge. diff -r 5e42deadf773 -r e48985ef1aac package.lisp --- a/package.lisp Sun May 05 11:40:57 2019 -0400 +++ b/package.lisp Sun May 05 11:41:36 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 diff -r 5e42deadf773 -r e48985ef1aac src/hash-tables.lisp --- a/src/hash-tables.lisp Sun May 05 11:40:57 2019 -0400 +++ b/src/hash-tables.lisp Sun May 05 11:41:36 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)