2,875
edits
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local | local Enumerable = require('Module:Enumerable') | ||
-- This is required to allow chaining if Enumerable functions in a FP manner. | |||
local EnumerableExtensions = require('Module:EnumerableExtensions') | |||
local TableEnumerator = require('Module:TableEnumerator') | |||
local | ---A wrapper for a Lua Table type that turns it into an Enumerable. Allows iteration over the table contents via a state-machine. | ||
---@class TableIterator : Enumerable | |||
---@field _tbl table | |||
local TableIterator = setmetatable({}, { __index = Enumerable }) | |||
TableIterator.__index = TableIterator | |||
TableIterator.__pairs = Enumerable.getPairs | |||
TableIterator.__ipairs = Enumerable.getiPairs | |||
function | function TableIterator.new(tbl) | ||
local self = setmetatable(Enumerable.new(), TableIterator) | |||
-- Allow creation of empty enumerable | |||
self._tbl = tbl or {} | |||
return self | |||
end | |||
function TableIterator:getEnumerator(isArray) | |||
return TableEnumerator.create(self._tbl, isArray) | |||
end | |||
-- Collection of shortcut functions so we don't have to iterate over all of the data | |||
-- in case the target enumerable is a TableIterator. | |||
function TableIterator:getCount() | |||
return #self._tbl | |||
end | |||
function TableIterator:getLast() | |||
return self._tbl[#self._tbl] | |||
end | |||
function TableIterator:getTable() | |||
return self._tbl | |||
end | end | ||
return | return TableIterator |
edits