我需要提前退出maphash
当我找到我要找的东西时。
(defun find-in-hash (str hash)
(let ((match nil))
(maphash (lambda (key value)
(if (string-prefix-p str key)
(setq match key))) hash)
match))
我该如何在 Emacs Lisp 中做到这一点?
正如中所解释的如何中断maphash http://osdir.com/ml/lisp.allegro/2006-04/msg00018.html你可以放置一个maphash
在块内并通过以下方式退出块return-from
,即使用以下形式
(block stop-mapping
(maphash
;; Function to call for all entries in ht.
;; A condition for when to stop mapping.
(return-from stop-mapping)
ht))
请注意,这需要cl
可以通过以下方式要求(require 'cl)
. 正如评论中提到的 https://stackoverflow.com/questions/15942513/how-to-break-out-from-maphash-in-emacs-lisp#comment22725803_15942774在纯 elisp 中可以通过以下方式实现相同的结果
(catch 'stop-mapping
(maphash
;; Function to call for all entries in ht.
;; A condition for when to stop mapping.
(throw 'stop-mapping retval)
ht))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)