问题描述
<$ p $ <$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p> (defun find-in-hash(str hash)
(let((match nil))
(maphash(lambda(key value))
string-prefix-p str key)
(setq match key)))hash)
match))
如何在Emacs Lisp中执行此操作?
正如,您可以放置一个 maphash
,并通过返回从
退出块,即使用表单
(块停止映射
(maphash
;;调用ht中的所有条目的函数
;;何时停止映射的条件
(从停止映射返回)
ht))
请注意,这需要 cl
可以通过(require'cl)
。 相同的结果可以通过
(catch'stop-mapping
(maphash
;; Function调用ht
;;何时停止映射的条件
(throw'stop-mapping)
ht))
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?
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)
ht))
这篇关于如何从Emacs Lisp的maphash中脱颖而出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!