• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Difference between revisions of "Module:Iterator"

From Kancolle Wiki
Jump to navigation Jump to search
(Created page with "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 ...")
 
Line 14: Line 14:
 
     end
 
     end
 
end
 
end
 +
 +
function Iterator.equipmentById(context)
 +
    local from = context and context.from and tonumber(context.from) or 1
 +
    local to = context and context.to and tonumber(context.to) or 500
 +
    local collection = require('Module:Collection/Equipment')
 +
    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.test()
 +
    local iterator = Iterator.equipmentById({ from = "11", to = "20" })
 +
    while iterator.next() do
 +
        mw.log(iterator.current())
 +
    end
 +
end
 +
-- p.test()
 +
]]--
  
 
return Iterator
 
return Iterator

Revision as of 19:14, 6 October 2017

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

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)
    local from = context and context.from and tonumber(context.from) or 1
    local to = context and context.to and tonumber(context.to) or 500
    local collection = require('Module:Collection/Equipment')
    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.test()
    local iterator = Iterator.equipmentById({ from = "11", to = "20" })
    while iterator.next() do
        mw.log(iterator.current())
    end
end
-- p.test()
]]--

return Iterator