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

table.countvalues #553

Open
Sainan opened this issue Dec 15, 2023 · 4 comments
Open

table.countvalues #553

Sainan opened this issue Dec 15, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Sainan
Copy link
Collaborator

Sainan commented Dec 15, 2023

function table.countvalues(t)
    local counts = {}
    for t as elm do
        counts[elm] = (counts[elm] ?? 0) + 1
    end
    return counts
end
@Sainan Sainan added the enhancement New feature or request label Dec 15, 2023
@well-in-that-case
Copy link
Collaborator

Could consider table.values() instead that returns an array of values

@Sainan
Copy link
Collaborator Author

Sainan commented Dec 16, 2023

Wdym?

@well-in-that-case
Copy link
Collaborator

well-in-that-case commented Dec 16, 2023

local assert = require("assert")
local t = { a = 1, b = 2, c = 3 }
assert.equals({ 1, 2, 3 }, t:values())

Then you could query for length if you needed.

@Sainan
Copy link
Collaborator Author

Sainan commented Dec 16, 2023

How does that solve this?
image

@Sainan Sainan modified the milestone: 0.9.0 Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants