Let's say I wrote
(setf s (make-array (list 9 9) :element-type 'bit))
so s is a 9x9 matrix of bits.
and I want to get the 1st row of s. How do I get that?
I could have done the following:
(setf s (make-array 9
:element-type 'array
:initial-element
(make-array 9 :element-type 'bit)))
and access the first row by (svref s 0).
But I want to know if there is a built-in way.
(And the 2 dim array seems to allocate less bytes).
(defun array-slice (arr row)
(make-array (array-dimension arr 1)
:displaced-to arr
:displaced-index-offset (* row (array-dimension arr 1))))
This only works for row slices and doesn't, IIRC, copy the array. Writing to the slice will modify the original array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With