How to break out from maphash in Emacs Lisp?
elisp, emacs
Solution
As explained in how to interrupt maphash you can place a `maphash` inside a block and exit the block via `return-from`, i.e. use the form
(block stop-mapping
(maphash
;; Function to call for all entries in ht.
;; A condition for when to stop mapping.
(return-from stop-mapping)
ht))
Note that this requires `cl` which can be required via `(require 'cl)`. As mentioned in a comment the same result can be achieved in pure elisp via
(catch 'stop-mapping
(maphash
;; Function to call for all entries in ht.
;; A condition for when to stop mapping.
(throw 'stop-mapping retval)
ht))
Problem
I need to exit early from `maphash` when I've found what I was looking for. ``` (defun find-in-hash (str hash) (let ((match nil)) (maphash (lambda (key value) (if (string-prefix-p str key) (setq match key))) hash) match)) ``` How would I do this in Emacs Lisp?