PlutoLang / Pluto

A superset of Lua 5.4 with a focus on general-purpose programming.
https://pluto-lang.org
MIT License
369 stars 22 forks source link

table.deduplicate(d) #554

Open Sainan opened 10 months ago

Sainan commented 10 months ago

Removes duplicate values from a table

well-in-that-case commented 10 months ago

Seems like a niche enough operation for filter + another table to handle tbh

Sainan commented 10 months ago

Ehhh

table.uniques = function(t)
    local seen = {}
    t:filter(function(x)
        if seen:contains(x) then return false end
        seen:insert(x)
        return true
    end)
    return t
end
Sainan commented 9 months ago

Although might need a different name such as table.deduplicate or table.dedup so we can have table.deduplicated or table.deduped for the copying variant.

Sainan commented 2 weeks ago

Somewhat worth noting that now this can be emulated as followed on 0.10.0:

local t = { "foo", "foo", "bar" }
print(dumpvar(t:countvalues():keys())) -- { "foo", "bar" }

Although probably still worthwhile to have its own function.