Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array2d indexing - allow str input to automatically parse range #399

Closed
kalekje opened this issue Dec 26, 2021 · 2 comments
Closed

array2d indexing - allow str input to automatically parse range #399

kalekje opened this issue Dec 26, 2021 · 2 comments

Comments

@kalekje
Copy link
Contributor

kalekje commented Dec 26, 2021

Before I start a fork/pull request, I just wanted to see interest in this idea,

We can index a 2d array with ints, or use the parse_range on a string. Can we allow this to be done automatically if i1 is a string?

function array2d.default_range (t,i1,j1,i2,j2)
 -- my patch
    if (type(i1) == 'string') and not (j1 or i2 or j2) then
        i1, j1, i2, j2 = array2d.parse_range(i1)
    end
--
    local nr, nc = array2d.size(t)
    i1 = norm_value(i1 or 1, nr)
    j1 = norm_value(j1 or 1, nc)
    i2 = norm_value(i2 or nr, nr)
    j2 = norm_value(j2 or nc, nc)
    return i1,j1,i2,j2
end

Additionally, I would like to add another flavor of string indexing: numpy-like indexing. I have the method worked out and would like to implement it. Currently, I cannot patch the array2d.default_range (after loading penlight that is, without modifying the source code) because the usage of default_range uses the local function default_range.. which begs the question, why does array2d.default_range exist then? I propose that we remove the local default_range and instead use the module function array2d.default_range for all uses.

@Tieske
Copy link
Member

Tieske commented Jan 4, 2022

We can index a 2d array with ints, or use the parse_range on a string. Can we allow this to be done automatically if i1 is a string?

That does make sense to me. PR welcome!

which begs the question, why does array2d.default_range exist then?

Locals are faster, hence internally we use a local, and we also export the function on the module for external uses.

@Tieske
Copy link
Member

Tieske commented Jan 5, 2022

closing in favour of #404

@Tieske Tieske closed this as completed Jan 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants