Copy circular list to fixed length list

I store the coordinates of the graph in a circular list:

(defun make-circular-list (size &key initial-element)
  (let ((list (make-list size :initial-element initial-element)))
    (nconc list list)))

(defvar *coordinates* (make-circular-list 1024 :initial-element 0.0))

      

It is now easy to update *coordinates*

when new coordinates have to be set.

However, I have a library function that takes a sequence of coordinates to draw lines on a graph. Of course this function does not work with circular structure, so I would like to make a fixed length copy. List or array is ok.

So far I have tried subseq

and make-array

the keyword :initial-contents

, but it fails. loop

or dotimes

work, but I was hoping to avoid iterating over each element of the list.

Is it possible to efficiently copy this circular structure or make a fixed length array in the spirit of a moved array?

+3


source to share


1 answer


There is nothing wrong with using LOOP.

(loop for c in *coordinates* repeat 1024 collect c)

      

Btw., Sometimes it can be useful to hide the circular list behind a CLOS object.



(defclass circular-list ()
   ((list)
    (size)
    (first-element)
    (last-element)))

      

Etc...

This way you can provide some CLOS methods for accessing and modifying (create, add, copy, delete, as-list, ...). You can also control its printing using the method PRINT-OBJECT

.

+5


source







All Articles