Lua-Collections icon indicating copy to clipboard operation
Lua-Collections copied to clipboard

Method to return one of each unique items in the collection

Open imliam opened this issue 8 years ago • 1 comments

It would be beneficial to have a method that returns only one of each item in the collection that matches a particular value.

collect({1, 1, 2, 2, 3, 4, 2}):methodName():all()
-- {1, 2, 3, 4}

collect({
    { animal = 'Cat', name = 'Bob' },
    { animal = 'Dog', name = 'Tim' },
    { animal = 'Cat', name = 'Beverly' }
}):methodName('animal'):all()
--[[
    {
        { animal = 'Cat', name = 'Bob' },
        { animal = 'Dog', name = 'Tim' }
    }
]]

collect({
    { animal = 'Cat', name = 'Bob' },
    { animal = 'Dog', name = 'Tim' },
    { animal = 'Cat', name = 'Beverly' }
}):methodName(function(key, value)
    return value:sub(1,1)
end):all()
--[[
    {
        { animal = 'Cat', name = 'Bob' },
        { animal = 'Dog', name = 'Tim' }
    }
]]

imliam avatar May 17 '17 15:05 imliam

Hi, and thanks for this awesome library! In many other collections libs, this method is called distinct. And IMO the library is really lacking it :)

anatoliykmetyuk avatar Jul 23 '18 21:07 anatoliykmetyuk