【发布时间】:2021-04-04 17:21:12
【问题描述】:
假设我有这个功能
(defun bar (a &optional (b nil bp))
(declare (ignore a b bp)) ; <-- replace it with (list a b bp)
; to check for correctness
)
;; I want to preserve the value of bp here inside bar, and inside foo below
我希望围绕bar写一个包装器:
(defun foo (a &optional (b nil bp))
(declare (optimize speed))
(apply #'bar a (nconc (when bp (list b)))))
同时在从foo 到bar 的调用中保留bp 的值,同时保持参数b 在emacs 的迷你缓冲区/eldoc 模式中可见。我想知道是否有一种可能不可移植的非 consing 方法来在此调用中保留 bp 的值。
例如,
CL-USER> (time (loop repeat 1000000 do (foo 4 2)))
Evaluation took:
0.040 seconds of real time
0.043656 seconds of total run time (0.043656 user, 0.000000 system)
110.00% CPU
96,380,086 processor cycles
15,990,784 bytes consed <--- plenty of consing
NIL
如果我忽略 b-visibility-in-eldoc,我可能会使用 &rest 参数,但我确实希望参数可见。
虽然在这种特殊情况下还有其他方法可以实现这一点,但我确实想考虑存在多个 &optional(或 &keyword)参数的情况。
【问题讨论】:
-
你为什么使用
nconc?只需写(apply #'bar a (when bp (list b))))。 -
@adabsurdum 在这个特定的例子中, nconc 没有相关性,是的;当我写它时,我打算将它用于可能有多个可选参数的情况,比如另一个
(c nil cp)