Common Lisp - 是否有内置函数可以按键过滤 plist?
Common Lisp - Is there a built-in function to filter a plist by keys?
我正在寻找像下面的 pfilter-by-keys
这样的内置函数:
(pfilter-by-keys '(:a :b) '(:c 10 :b 20 :a 4))
;; => (:B 20 :A 4)
它的代码很简单:
(defun pfilter-by-keys (keys plist)
"List -> PList -> PList
Returns a new plist with only the keys/values correspondent to the given
keys."
(loop for (k v) on plist by #'cddr
when (member k keys :test #'equal)
append (list k v)))
CL 是否有像上面那样的一些内置功能?
PS.: Alexandria 有一个非常接近的函数:remove-from-plist
.
(defun keep-properties (plist indicator-list &aux k v)
"Keeps all property list entries for a given indicator-list."
(loop do (setf (values k v plist)
(get-properties plist indicator-list))
while plist
collect k collect v
do (pop plist) (pop plist)))
请注意,收集两次比 append/list 在 LOOP
中收集更好。
没有执行此操作的功能(这不是人们通常想做的事情)
有一个宏 remf
可以从某个地方的 plist 中删除一个键。
实现此目的的另一种方法是:
(destructuring-bind (&key (a nil ap) (b nil bp) &allow-other-keys) plist
(append (if ap (list :a a)) (if bp (list :b b))))
但请注意,这仅在您已经知道要保留哪些键的情况下才有效,并且不会保留 plist 中的顺序,也不会保留重复的键(即如果您的 plist 包含键 :a
多次结果将只包含它一次)。
您可以使用普通(符号)键的普通 lambda 列表语法为非关键字键修改此设置。
我正在寻找像下面的 pfilter-by-keys
这样的内置函数:
(pfilter-by-keys '(:a :b) '(:c 10 :b 20 :a 4))
;; => (:B 20 :A 4)
它的代码很简单:
(defun pfilter-by-keys (keys plist)
"List -> PList -> PList
Returns a new plist with only the keys/values correspondent to the given
keys."
(loop for (k v) on plist by #'cddr
when (member k keys :test #'equal)
append (list k v)))
CL 是否有像上面那样的一些内置功能?
PS.: Alexandria 有一个非常接近的函数:remove-from-plist
.
(defun keep-properties (plist indicator-list &aux k v)
"Keeps all property list entries for a given indicator-list."
(loop do (setf (values k v plist)
(get-properties plist indicator-list))
while plist
collect k collect v
do (pop plist) (pop plist)))
请注意,收集两次比 append/list 在 LOOP
中收集更好。
没有执行此操作的功能(这不是人们通常想做的事情)
有一个宏 remf
可以从某个地方的 plist 中删除一个键。
实现此目的的另一种方法是:
(destructuring-bind (&key (a nil ap) (b nil bp) &allow-other-keys) plist
(append (if ap (list :a a)) (if bp (list :b b))))
但请注意,这仅在您已经知道要保留哪些键的情况下才有效,并且不会保留 plist 中的顺序,也不会保留重复的键(即如果您的 plist 包含键 :a
多次结果将只包含它一次)。
您可以使用普通(符号)键的普通 lambda 列表语法为非关键字键修改此设置。