2,875
edits
No edit summary |
No edit summary |
||
Line 273: | Line 273: | ||
local key = keySelector(item) | local key = keySelector(item) | ||
local val = valueSelector(item) | local val = valueSelector(item) | ||
if dict[key] then | |||
error('Duplicate key error.') | |||
end | |||
dict[key] = val | dict[key] = val | ||
end | end | ||
Line 279: | Line 283: | ||
local newFL = funlist.new(dict) | local newFL = funlist.new(dict) | ||
return newFL | return newFL | ||
end | |||
function funlist:distinct() | |||
local hashSet = {} | |||
local uniques = {} | |||
-- Filter out unique values. | |||
for _, v in pairs(self.mytable) do | |||
if not hashSet[v] then | |||
hashSet[v] = true | |||
table.insert(uniques, v) | |||
end | |||
end | |||
self.mytable = uniques | |||
return self | |||
end | |||
function funlist:distinctBy(selector) | |||
assert(selector) | |||
local hashSet = {} | |||
local uniques = {} | |||
-- Filter out unique values. | |||
for _, item in pairs(self.mytable) do | |||
local v = selector(item) | |||
if not hashSet[v] then | |||
hashSet[v] = true | |||
table.insert(uniques, v) | |||
end | |||
end | |||
self.mytable = uniques | |||
return self | |||
end | end | ||
edits