2,875
edits
(toDictionary returns a table instead of a FL) |
(Add function overrides (as far as possible in Lua anyway) to first and firstordefault) |
||
Line 110: | Line 110: | ||
-- Returns the first element of a sequence. | -- Returns the first element of a sequence. | ||
function funlist:first() | function funlist:first(predicate) | ||
for _, v in pairs(self.mytable) do | if predicate then | ||
return v | for _, v in pairs(self.mytable) do | ||
if predicate(v) == true then | |||
return v | |||
end | |||
end | |||
else | |||
for _, v in pairs(self.mytable) do | |||
return v | |||
end | |||
end | end | ||
error('No items in sequence.') | error('No items in sequence.') | ||
Line 118: | Line 126: | ||
-- Returns the first element of a sequence, or a default value if no element is found. | -- Returns the first element of a sequence, or a default value if no element is found. | ||
function funlist:firstOrDefault(defaultItem) | function funlist:firstOrDefault(predicate, defaultItem) | ||
for _, v in pairs(self.mytable) do | if predicate then | ||
return v | for _, v in pairs(self.mytable) do | ||
if predicate(v) == true then | |||
return v | |||
end | |||
end | |||
else | |||
for _, v in pairs(self.mytable) do | |||
return v | |||
end | |||
end | end | ||
return defaultItem | return defaultItem |
edits