用于过滤列表并将函数应用于未过滤元素的 Common Lisp 习惯用法?

Common Lisp idiom for filtering a list and applying a function to the unfiltered elements?

Common Lisp 的成语是什么:

Remove the elements in a list that don't satisfy a predicate and for those that do satisfy the predicate apply a function.

这是 Common Lisp 习语吗:

mapcar applied to remove-if

成语是MAPCAR over REMOVE-IF-NOT, since you want to keep elements that match the predicate. The -IF-NOT functions are deprecated according to the standard, but for Common Lisp deprecation is mostly meaningless1 and as such we rarely see anyone use COMPLEMENT with REMOVE-IF

但是大多数人会在这里使用 LOOP

(lambda (elements test function)
  (loop
    for e in elements
    when (funcall test e)
      collect (funcall function e)))

MAPCAR over REMOVE-IF-NOT is that it is going to allocate memory for a temporary list just to discard it after. You can call this premature optimisation, but if I wanted to use higher-order functions (e.g. because I want to work with generalised sequences instead of just lists), I would use MAP-INTO 可能存在的问题:

(lambda (elements test function)
  (let ((tmp (remove-if-not test elements)))
    (map-into tmp function tmp)))

1. "所有被标记为已弃用的内容都可以视为未弃用,因为不会有另一个标准。", R. Strandh (beach).