You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(format t "The value associated with the key ~S is ~S~%" key value)
664
+
(return)))))
634
665
;; =>
635
-
(A 1)
636
-
(B 2)
666
+
The value associated with the key A is 1
667
+
The value associated with the key B is 2
637
668
NIL
638
669
~~~
639
670
671
+
Note the following caveat from the HyperSpec:
640
672
641
-
#### maphash
673
+
> It is unspecified what happens if any of the implicit interior state
674
+
> of an iteration is returned outside the dynamic extent of the
675
+
> `with-hash-table-iterator` form such as by returning some closure
676
+
> over the invocation form.
642
677
643
-
The lambda function of `maphash` takes two arguments: the key and the
644
-
value:
678
+
#### iterate: `in-hashtable`
679
+
680
+
Use `in-hashtable`:
645
681
646
682
~~~lisp
647
-
(maphash (lambda (key val)
648
-
(format t "key: ~A value: ~A~%" key val))
649
-
h)
683
+
(iter (for (k v) in-hashtable *my-hash-table*)
684
+
(collect (list k v)))
685
+
;; ((B 2) (A 1))
686
+
~~~
687
+
688
+
#### Alexandria's `maphash-keys` and `maphash-values`
689
+
690
+
To map over keys or values (and only keys or only values) we can again
691
+
rely on Alexandria with `maphash-keys` and `maphash-values`.
692
+
693
+
#### Serapeum's `do-hash-table`
694
+
695
+
The [Serapeum library](https://github.com/ruricolist/serapeum/blob/master/REFERENCE.md#hash-tables) has a do-like macro called `do-hash-table`.
696
+
697
+
(do-hash-table (key value table &optional return) &body body)
698
+
699
+
700
+
#### for
701
+
702
+
With the `for` library, use the `over` keyword:
703
+
704
+
~~~lisp
705
+
(for:for ((it over *my-hash-table*))
706
+
(print it))
650
707
;; =>
651
-
key: A value: 1
652
-
key: B value: 2
708
+
(A 1)
709
+
(B 2)
653
710
NIL
654
711
~~~
655
712
656
-
See also [with-hash-table-iterator](http://www.lispworks.com/documentation/HyperSpec/Body/m_w_hash.htm).
657
-
658
-
#### dohash
713
+
#### trivial-do:dohash
659
714
660
715
Only because we like this topic, we introduce another library, [trivial-do](https://github.com/yitzchak/trivial-do/). It has the `dohash` macro, that ressembles `dolist`:
0 commit comments