Common Lisp有一些功能.我如何写一些或任何elisp?

Che*_*eso 1 emacs elisp map

一个any功能将评估上的序列的元素的函数(或谓词),如果断言返回该序列的任何元件真正返回true.因此

(any `stringp `(42 t "yes" nil))
Run Code Online (Sandbox Code Playgroud)

..将返回非零,并且

(any `stringp `(42 t nil 89))
Run Code Online (Sandbox Code Playgroud)

...将返回零.

我认为someCL模块中有一个功能,但我不想使用它.

使用mapc编写一个简单的实现很容易:

(defun my-any (predicate sequence)
  "Return true if PREDICATE is true of any element of SEQUENCE.
If so, return the first non-nil value returned by PREDICATE.
"
  (let (result)
    (mapc '(lambda (elt)
             (or result
                 (setq result (funcall predicate elt))))
          sequence)
    result))
Run Code Online (Sandbox Code Playgroud)

但是,任何人都可以提供优化或改进吗?是否有更快或更清洁或更惯用的elisp方式来做到这一点?特别是当第一个序列触发时,停止映射序列的最佳方法是什么?我可以"展开"mapc,但这是最好的方法吗?

我想我可以使用catch但实际上可能比它节省的成本更多.

提示?

hua*_*uan 5

(defun any (pred list)
  (while (and list (not (funcall pred (car list))))
    (pop list))
  (car list))
Run Code Online (Sandbox Code Playgroud)