如何从Emacs Lisp的maphash中脱颖而出? [英] How to break out from maphash in Emacs Lisp?

查看:94
本文介绍了如何从Emacs Lisp的maphash中脱颖而出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ 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 ,并通过返回从退出块,即使用表单

 (块停止映射
(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中脱颖而出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆