Module:Iterator

Revision as of 11:28, 8 October 2017 by がか (talk | contribs)

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

-- [[Category:Todo]]:
-- more generic interface and compositions (filtering, grouping, mapping, sorting, etc.)
-- prevent clients from infinite loops

local Iterator = {}

function Iterator.array(arr, i, n)
    i = i or 0
    n = n and math.min(#arr, n) or #arr
    function step(n, i)
        if i < n then
            i = i + 1
            return i, arr[i]
        end
    end
    return function()
        return step, n, i
    end
end

function Iterator.equipmentById(context, n)
    local fromKey = "from" .. (n and tostring(n) or "")
    local from = context and context[fromKey] and tonumber(context[fromKey]) or 1
    local toKey = "to" .. (n and tostring(n) or "")
    local to = context and context[toKey] and tonumber(context[toKey]) or 500
    local sortKey = "sort" .. (n and tostring(n) or "")
    local sort = context and context[sortKey]
    local collection = require('Module:Collection/Equipment')
    if sort then
        table.sort(collection, function(a, b) return a[sort] < b[sort] end)
    end
    local i = 1
    local current
    return {
        next = function()
            for _ = i, #collection do
                if i >= from and i <= to then
                    current = collection[i]._name
                    i = i + 1
                    return true
                end
                i = i + 1
            end
            current = nil
            return false
        end,
        current = function()
            return current
        end,
    }
end

function Iterator.equipment(n)
    return Iterator.equipmentById(nil, n)
end

function Iterator.equipmentByType(context, n)
    local typeKey = "type" .. (n and tostring(n) or "")
    local type = context and context[typeKey] and tonumber(context[typeKey])
    local sortKey = "sort" .. (n and tostring(n) or "")
    local sort = context and context[sortKey]
    local collection = require('Module:Collection/Equipment')
    if sort then
        table.sort(collection, function(a, b) return a[sort] < b[sort] end)
    end
    local i = 1
    local current
    return {
        next = function()
            for _ = i, #collection do
                if collection[i]._type == type then
                    current = collection[i]._name
                    i = i + 1
                    return true
                end
                i = i + 1
            end
            current = nil
            return false
        end,
        current = function()
            return current
        end,
    }
end

function Iterator.equipmentByTypeAndIcon(context, n)
    local typeKey = "type" .. (n and tostring(n) or "")
    local iconKey = "icon" .. (n and tostring(n) or "")
    local type = context and context[typeKey] and tonumber(context[typeKey])
    local icon = context and context[iconKey] and tonumber(context[iconKey])
    local sortKey = "sort" .. (n and tostring(n) or "")
    local sort = context and context[sortKey]
    local collection = require('Module:Collection/Equipment')
    if sort then
        table.sort(collection, function(a, b) return a[sort] < b[sort] end)
    end
    local i = 1
    local current
    return {
        next = function()
            for _ = i, #collection do
                if collection[i]._type == type and collection[i]._icon == icon then
                    current = collection[i]._name
                    i = i + 1
                    return true
                end
                i = i + 1
            end
            current = nil
            return false
        end,
        current = function()
            return current
        end,
    }
end

--[[
function Iterator.test()
    local iterator = Iterator.equipmentById({ from = "11", to = "20" })
    while iterator.next() do
        mw.log(iterator.current())
    end
end
-- p.test()
]]--

return Iterator