Generic Lisp: Effectively adding a nested plist

I use nested plist to create an object structure (CLOS type), passing nested parts to my parts. I want to add nested plist iteratively, but so I want to do this efficiently in terms of time and memory.

The example below shows the delta due to one iteration:

'(:airframer "Boeing" :type "777" :wing-plist ((:side :left :winglet? nil) (:side :right :winglet? nil))) 

in

 '(:airframer "Boeing" :type "777" :wing-plist ((:type :main-wing :side :left) (:type :main-wing :side :right) (:type :stabilizer :size :left))) 

I already read that using vectors instead of lists can help, since you are accessing elements without much penalty: Replace an element in a list in Common Lisp? . However, I would really like to get around using vectors.

In addition, I believe that using a destructive function will save memory and, hopefully, computation time.

This is how I decided it now, but I have the feeling that it is not elegant and effective. The fill function is used for destructiveness.

 (defun append-nested-plist (plist key sub-plist) (let* ((key-pos (position key plist))) (fill plist (append (getf plist key) (list sub-plist)) :start (+ key-pos 1) :end (+ key-pos 2)))) 

I look forward to your answers.

+6
source share
1 answer

How about this?

 (defun append-nested-plist (plist key sub-plist) (push-to-end sub-plist (getf plist key)) plist) 

Push-to-end is a commonly defined macro that is not part of the standard lisp standard:

 (defmacro push-to-end (item place) `(setf ,place (nconc ,place (list ,item)))) 
+3
source

Source: https://habr.com/ru/post/903767/


All Articles