Skip to content

Suggestions for rewrite of Matrix library #9

@dcuny

Description

@dcuny

There are a couple of Lua idioms that you might want to take advantage of for the Matrix library.

First, you can get the count of elements in a table t with the # operator.

So to get the count of rows in a table, you can write:

local rows = #t

Similarly, you can get the count of columns in the table by getting the count from the first row:

local cols = #t[1]

So instead of storing the rows and columns, you could write:

-- get the number of rows and columns
local rows, cols = #m1, #m1[1]

If you're going to be iterating over a 2 dimensional table, it's a bit faster to place the row in a variable. You can then index into the row with row[j], which is slightly faster (but perhaps less easy to read).

For example, instead of:

for i = 1, rows do
  for j = 1, cols do
    foo( t[i][j] )
  end
end

You can use the ipairs iterator, which returns the index and value from the table:

for i, row in ipairs( t ) do
  for j, col in ipairs( row ) do
    foo( col )
  end
end

On the other hand this sort of idiom can make the code harder to read.

Here's a rewrite of the Matrix library using those idoms: Matrix.txt

I'm not saying that it's any better than what you wrote, it's just something to consider.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions