gpt4 book ai didi

emacs - 如何从 Emacs Lisp 中的 maphash 中脱颖而出?

转载 作者:行者123 更新时间:2023-12-02 06:37:10 25 4
gpt4 key购买 nike

当我找到我要找的东西时,我需要提前从 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 中做到这一点?

最佳答案

how to interrupt maphash 中所述您可以将 maphash 放置在一个 block 内并通过 return-from 退出该 block ,即使用表单

(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) 要求。 As mentioned in a comment通过

在纯 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))

关于emacs - 如何从 Emacs Lisp 中的 maphash 中脱颖而出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com