Trade-off, option, choice.



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.

I started writing the accessors, but I'm worried about
the amount of duplicate code. To wit:

(defun get-grid-row (grid row)
"Returns the given row of the grid as a string"
(let* ((length (grid-cols grid))
(s (make-array length :fill-pointer 0 :adjustable t :element-type 'character)))
(dotimes (col length s)
(vector-push-extend (grid@ grid col row) s))))


(defun get-grid-col (grid col)
"Returns the given column of the grid as a string"
(let* ((length (grid-rows grid))
(s (make-array length :fill-pointer 0 :adjustable t :element-type 'character)))
(dotimes (row length s)
(vector-push-extend (grid@ grid col row) s))))


This piece of code is exactly the same one, save for the swaping
of row & cols.

And that's a simple example. I've got a much more involved piece of code,
which I already wrote, debugged and tested for the row case, and I really
don't feel like copy-paste-patch a second instance.

The choices I see are:

1. have the coordinate include its orientation. Like > 0 for cols
  and < 0 for rows.
  -> this wouldn't easily scale to 3 dimensions(but I don't care)
  -> this pushes an explicit test (against 0) at the lowest level

2. use an array of two elements, and transform all references
  of the form (foo col), (foo row) into (foo index direction)
  -> this would increase the argument list by one everywhere.
  -> replacing a pair of (row, col) slots with an array of two
     elements is going to be more expensive both in time and
     space, but not emphatically so,
  -> I can defer chosing between the :by-column and :by-row
     directions until the highest level.

3. use copy-paste-patch as I've started doing. But I'm not pleased
  at all. this smells.

Anyone see any other options I missed?

Many Thanks
--
JFB

.



Relevant Pages