Re: Trade-off, option, choice.



verec wrote:
I've got a two dimensional grid of characters.

At one stage, I need to extract strings from the grid,
either on a row by row basis, or on a column by column
basis.

[...]

Anyone see any other options I missed?

I' m a big fan of (1) the :displaced-to feature of <make-array>, and (2) <loop>.


Carl Taylor


(defun get-char-grid-row (row x-grid) (declare (type fixnum row)) (let ((col-cnt (array-dimension x-grid 1))) (map 'string #'identity (make-array col-cnt :element-type (array-element-type x-grid) :displaced-to x-grid :displaced-index-offset (* row col-cnt)))))


(defun get-char-grid-col (col x-grid) (declare (type fixnum col)) (map 'string #'identity (loop :for i fixnum :repeat (the fixnum (array-dimension x-grid 0)) :collect (aref x-grid i col))))


CL-USER 13 > (get-char-grid-row 2 #2A((#\a #\b #\c) (#\d #\e #\f) (#\g #\h #\i))) "ghi"

CL-USER 14 >

(get-char-grid-col 1 #2A((#\a #\b #\c) (#\d #\e #\f) (#\g #\h #\i)))
"beh"







.