Module:FunList/Iterators: Difference between revisions

From Melvor Idle
m (Ricewind moved page Module:FunList/StateMachine to Module:FunList/Enumerators without leaving a redirect)
No edit summary
Line 1: Line 1:
enumerable = {}
-- BASE ENUMERATOR CLASS --
local enumerable = {}
--enumerable = {}
local enumerable_mt = {
local Enumerator = {}
__index = enumerable,
local Enumerator_mt = {
__index = Enumerator,
__pairs = function(t) return t:overridePairs()end
__pairs = function(t) return t:overridePairs()end
}
}


function enumerable.new(tbl)
function Enumerator.new()
local self = setmetatable({}, enumerable_mt)
local self = setmetatable({}, Enumerator_mt)
   
self.data = tbl
self.current = nil
self.current = nil
self.index = nil
self.index = nil
Line 15: Line 14:
end
end


function enumerable:moveNext()
function Enumerator:moveNext()
-- Grab the next index for the internal table.
error('Not implemented in base class.')
local key = self.index
key = next(self.data, key)
self.index = key
-- If the index exist, we have succesfuly moved to the next.
if key then
self.current = self.data[key]
return true
end
return false
end
end


function enumerable:overridePairs()
function Enumerator:overridePairs()
self.index = nil
self.index = nil
self.current = nil
self.current = nil
Line 44: Line 32:
end
end


return enumerable
-- TABLE ENUMERATOR CLASS --
-- This is essentially a wrapper for the table object,
-- since it provides no state machine esque iteration out of the box
local TableEnumerator = setmetatable({}, { __index = Enumerator })
TableEnumerator.__index = TableEnumerator
 
function TableEnumerator.new(tbl)
    local self = setmetatable(Enumerator.new(), TableEnumerator)
    self.data = tbl
    return self
end
 
function TableEnumerator:moveNext()
-- Grab the next index for the internal table.
local key = self.index
key = next(self.data, key)
self.index = key
-- If the index exist, we have succesfuly moved to the next.
if key then
self.current = self.data[key]
return true
end
return false
end
 
return {
    Enumerator = Enumerator,
    TableEnumerator = TableEnumerator
}

Revision as of 15:26, 21 July 2024

Documentation for this module may be created at Module:FunList/Iterators/doc

-- BASE ENUMERATOR CLASS --
--enumerable = {}
local Enumerator = {}
local Enumerator_mt = {
	__index = Enumerator,
	__pairs = function(t) return t:overridePairs()end
}

function Enumerator.new()
	local self = setmetatable({}, Enumerator_mt)
	self.current = nil
	self.index = nil
	return self
end

function Enumerator:moveNext()
	error('Not implemented in base class.')
end

function Enumerator:overridePairs()
	self.index = nil
	self.current = nil
	local function iterator(t, k)
		if self:moveNext() == true then
			return self.index, self.current
		else
			return nil, nil
		end
	end
	
	return iterator, self, nil
end

-- TABLE ENUMERATOR CLASS --
-- This is essentially a wrapper for the table object, 
-- since it provides no state machine esque iteration out of the box
local TableEnumerator = setmetatable({}, { __index = Enumerator })
TableEnumerator.__index = TableEnumerator

function TableEnumerator.new(tbl)
    local self = setmetatable(Enumerator.new(), TableEnumerator)
    self.data = tbl
    return self
end

function TableEnumerator:moveNext()
	-- Grab the next index for the internal table.
	local key = self.index
	key = next(self.data, key)
	self.index = key
	
	-- If the index exist, we have succesfuly moved to the next.
	if key then
		self.current = self.data[key]
		return true
	end
	
	return false
end

return {
    Enumerator = Enumerator,
    TableEnumerator = TableEnumerator
}