2,875
edits
No edit summary |
(Add WhereEnumerator) |
||
Line 25: | Line 25: | ||
-- Hooks the moveNext function into the Lua 'pairs' function | -- Hooks the moveNext function into the Lua 'pairs' function | ||
function Enumerator:overridePairs() | function Enumerator:overridePairs() | ||
self.index = nil | self.index = nil | ||
self.current = nil | self.current = nil | ||
Line 95: | Line 94: | ||
function SelectEnumerator:moveNext() | function SelectEnumerator:moveNext() | ||
if self.state == 0 then | if self.state == 0 then | ||
self.state = 1 | self.state = 1 | ||
Line 121: | Line 119: | ||
else | else | ||
return SelectEnumerator.new(self.source, self.selector) | return SelectEnumerator.new(self.source, self.selector) | ||
end | |||
end | |||
-- WHERE ENUMERATOR -- | |||
local WhereEnumerator = setmetatable({}, { __index = Enumerator }) | |||
WhereEnumerator.__index = WhereEnumerator | |||
WhereEnumerator.__pairs = Enumerator_mt.__pairs | |||
function WhereEnumerator.new(source, predicate) | |||
local self = setmetatable(Enumerator.new(), WhereEnumerator) | |||
self.state = 0 | |||
self.source = source | |||
self.predicate = predicate | |||
self.enumerator = nil | |||
return self | |||
end | |||
function WhereEnumerator:moveNext() | |||
if self.state == 0 then | |||
self.state = 1 | |||
self.position = 0 | |||
self.enumerator = self.source:getEnumerator() | |||
end | |||
if self.state == 1 then | |||
local enumerator = self.enumerator | |||
while enumerator:moveNext() == true do | |||
local sourceElement = enumerator.current | |||
if self.predicate(sourceElement) == true then | |||
self.index = enumerator.index | |||
self.current = sourceElement | |||
return true | |||
end | |||
end | |||
end | |||
return false | |||
end | |||
function WhereEnumerator:getEnumerator() | |||
if self.state == 0 then | |||
return self | |||
else | |||
return WhereEnumerator.new(self.source, self.selector) | |||
end | end | ||
end | end | ||
Line 127: | Line 169: | ||
Enumerator = Enumerator, | Enumerator = Enumerator, | ||
TableEnumerator = TableEnumerator, | TableEnumerator = TableEnumerator, | ||
SelectEnumerator = SelectEnumerator | SelectEnumerator = SelectEnumerator, | ||
WhereEnumerator = WhereEnumerator | |||
} | } |
edits