Anonymous

Module:FunList/Iterators: Difference between revisions

From Melvor Idle
Add ConcatEnumerator
(Finally added support for ipairs)
(Add ConcatEnumerator)
Line 110: Line 110:


function SelectEnumerator.new(source, selector)
function SelectEnumerator.new(source, selector)
assert(source, 'Source cannot be nil')
assert(selector, 'Selector cannot be nil')
     local self = setmetatable(Enumerator.new(), SelectEnumerator)
     local self = setmetatable(Enumerator.new(), SelectEnumerator)
     self.state = 0
     self.state = 0
Line 153: Line 155:


function WhereEnumerator.new(source, predicate)
function WhereEnumerator.new(source, predicate)
assert(source, 'Source cannot be nil')
assert(predicate, 'Predicate cannot be nil')
     local self = setmetatable(Enumerator.new(), WhereEnumerator)
     local self = setmetatable(Enumerator.new(), WhereEnumerator)
     self.state = 0
     self.state = 0
Line 189: Line 193:
end
end


-- WHERE ENUMERATOR --
-- SELECTMANY ENUMERATOR --
local SelectManyEnumerator = setmetatable({}, { __index = Enumerator })
local SelectManyEnumerator = setmetatable({}, { __index = Enumerator })
SelectManyEnumerator.__index = SelectManyEnumerator
SelectManyEnumerator.__index = SelectManyEnumerator
Line 196: Line 200:


function SelectManyEnumerator.new(source, selector)
function SelectManyEnumerator.new(source, selector)
assert(source, 'Source cannot be nil')
assert(selector, 'Selector cannot be nil')
     local self = setmetatable(Enumerator.new(), SelectManyEnumerator)
     local self = setmetatable(Enumerator.new(), SelectManyEnumerator)
     self.state = 0
     self.state = 0
Line 253: Line 259:
function SelectManyEnumerator:getEnumerator(isArray)
function SelectManyEnumerator:getEnumerator(isArray)
local instance = (self.state == 0) and self or SelectManyEnumerator.new(self.source, self.selector)
local instance = (self.state == 0) and self or SelectManyEnumerator.new(self.source, self.selector)
    instance.isArray = isArray
    return instance
end
-- CONCAT ENUMERATOR --
local ConcatEnumerator = setmetatable({}, { __index = Enumerator })
ConcatEnumerator.__index = ConcatEnumerator
ConcatEnumerator.__pairs = Enumerator_mt.__pairs
ConcatEnumerator.__ipairs = Enumerator_mt.__ipairs
function ConcatEnumerator.new(first, second)
assert(first, 'First cannot be nil')
assert(second, 'Second cannot be nil')
    local self = setmetatable(Enumerator.new(), ConcatEnumerator)
    self.state = 0
    self.first = first
    self.second = second
    self.enumerator = nil
    return self
end
-- Function to grab enumerators in order. We currently only have two so this
-- seems a bit redundant. But it would allow to add N-amount of enumerables.
function ConcatEnumerator:getEnumerable(index)
if index == 0 then
return self.first
elseif index == 1 then
return self.second
else
return nil
end
end
function ConcatEnumerator:moveNext()
if self.state == 0 then
self.enumerator = self:getEnumerable(self.state)
:getEnumerator(self.isArray)
self.state = 1
end
if self.state > 0 then
while true do
if self.enumerator:moveNext() == true then
self.index = self.enumerator.index
self.current = self.enumerator.current
return true
end
self.state = self.state + 1
local next = self:getEnumerable(self.state - 1)
if next ~= nil then
self.enumerator = next:getEnumerator(self.isArray)
else
return false
end
end
end
return false
end
function ConcatEnumerator:getEnumerator(isArray)
    local instance = (self.state == 0) and self or ConcatEnumerator.new(self.first, self.second)
     instance.isArray = isArray
     instance.isArray = isArray
     return instance
     return instance
Line 262: Line 331:
     SelectEnumerator = SelectEnumerator,
     SelectEnumerator = SelectEnumerator,
     WhereEnumerator = WhereEnumerator,
     WhereEnumerator = WhereEnumerator,
     SelectManyEnumerator = SelectManyEnumerator
     SelectManyEnumerator = SelectManyEnumerator,
    ConcatEnumerator = ConcatEnumerator
}
}
2,875

edits