Pluto icon indicating copy to clipboard operation
Pluto copied to clipboard

table.deduplicate(d)

Open Sainan opened this issue 2 years ago • 4 comments

Removes duplicate values from a table

Sainan avatar Dec 15 '23 21:12 Sainan

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

well-in-that-case avatar Dec 15 '23 23:12 well-in-that-case

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 avatar Dec 19 '23 04:12 Sainan

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 avatar Dec 19 '23 04:12 Sainan

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.

Sainan avatar Sep 26 '24 14:09 Sainan