Anonymous

Module:FunList/Iterators: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
No edit summary
Line 5: Line 5:
__index = Enumerator,
__index = Enumerator,
__pairs = function(t) return t:overridePairs() end,
__pairs = function(t) return t:overridePairs() end,
__ipairs = function(t) return t:overrideiPairs() end
--__ipairs = function(t) return t:overrideiPairs() end
}
}


Line 16: Line 16:


function Enumerator:moveNext()
function Enumerator:moveNext()
error('Not implemented in base class.')
end
function Enumerator:moveINext()
error('Not implemented in base class.')
error('Not implemented in base class.')
end
end
Line 35: Line 31:
self.current = nil
self.current = nil
return iterator, self, nil
return iterator, self, nil
end
-- Hooks the moveNext function into the Lua 'ipairs' function
function Enumerator:overrideiPairs()
    local function iterator(t, k)
    if self:moveINext() == true then
    return self.index, self.current
    end
    return nil, nil
    end
   
    self.index = 0
    self.current = nil
    return iterator, self, self.index
end
end


2,873

edits