896559ec54b1

Add do-hash-set
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 08 Feb 2023 21:27:09 -0500
parents bfa9e871b3c0
children 65478981d36d
branches/tags (none)
files DOCUMENTATION.markdown src/hash-sets.lisp src/package.lisp

Changes

--- a/DOCUMENTATION.markdown	Wed Feb 08 20:42:24 2023 -0500
+++ b/DOCUMENTATION.markdown	Wed Feb 08 21:27:09 2023 -0500
@@ -1327,6 +1327,14 @@
 
   
 
+### `DO-HASH-SET` (macro)
+
+    (DO-HASH-SET (SYMBOL HSET)
+      &BODY
+      BODY)
+
+Iterate over `hset` with `symbol` bound to successive elements.
+
 ### `HASH-SET` (struct)
 
 Slots: `STORAGE`
--- a/src/hash-sets.lisp	Wed Feb 08 20:42:24 2023 -0500
+++ b/src/hash-sets.lisp	Wed Feb 08 21:27:09 2023 -0500
@@ -229,3 +229,10 @@
   (alexandria:hash-table-keys storage))
 
 
+(defmacro do-hash-set ((symbol hset) &body body)
+  "Iterate over `hset` with `symbol` bound to successive elements."
+  (with-gensyms (iter found)
+    `(with-hash-table-iterator (,iter (hash-set-storage ,hset))
+       (loop (multiple-value-bind (,found ,symbol) (,iter)
+               (unless ,found (return nil))
+               ,@body)))))
--- a/src/package.lisp	Wed Feb 08 20:42:24 2023 -0500
+++ b/src/package.lisp	Wed Feb 08 21:27:09 2023 -0500
@@ -96,7 +96,9 @@
     :hset-filter!
     :hset-map
     :hset-map!
-    :hset-reduce))
+    :hset-reduce
+
+    :do-hash-set))
 
 
 (defpackage :losh.io