Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only the keys of a plist

Tags:

common-lisp

I do the following code to retrieve only the keys of a plist:

(loop :for (key nil) :on config :by #'cddr
      :collect key))

Running this produces:

CONFIG-TEST> (loop :for (key nil) :on '(:foo 1 :bar 2) :by #'cddr
                   :collect key)
(:FOO :BAR)

Is there a more 'functional' way to do this than using LOOP?

like image 559
Manfred Avatar asked Nov 24 '25 03:11

Manfred


1 Answers

Using the SERIES package, scan-plist returns two series, one for the keys, the other for values:

(scan-plist '(:a 3 :b 2))
=> #Z(:A :B)
   #Z(3 2)

You can rely on this to collect the first series as a list:

(collect 'list (scan-plist '(:a 3 :b 2)))

More generally, you may want to process the values in some way, so you would use mapping. For example, here is a plist-alist made with SERIES:

(defun plist-alist (plist)
  (collect 'list
    (mapping (((k v) (scan-plist plist))) (cons k v))))
like image 76
coredump Avatar answered Nov 25 '25 21:11

coredump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!