- 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"
Jump to navigation
Jump to search
m |
m |
||
Line 2: | Line 2: | ||
-- more generic interface and compositions (filtering, grouping, mapping, sorting, etc.) | -- more generic interface and compositions (filtering, grouping, mapping, sorting, etc.) | ||
-- prevent clients from infinite loops | -- prevent clients from infinite loops | ||
− | |||
− | |||
local Iterator = {} | local Iterator = {} | ||
Line 34: | Line 32: | ||
if predKey then | if predKey then | ||
pred = function(e) | pred = function(e) | ||
+ | local EquipmentData = require('Module:EquipmentData') | ||
local obj = EquipmentData(e) | local obj = EquipmentData(e) | ||
return obj[predKey](obj) | return obj[predKey](obj) |
Revision as of 11:52, 8 October 2017
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 = {}
-- legacy, not a proper 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 stringKey(name, context, n, default)
return context and context[name .. (n and tostring(n) or '')] or default
end
function numberKey(name, context, n, default)
return context and tonumber(context[name .. (n and tostring(n) or '')]) or default
end
function Iterator.equipmentBy(context, n, pred)
local predKey = stringKey('pred', context, n)
if predKey then
pred = function(e)
local EquipmentData = require('Module:EquipmentData')
local obj = EquipmentData(e)
return obj[predKey](obj)
end
end
local collection = require('Module:Collection/Equipment')
local sortKey = stringKey('sort', context, n)
if sortKey then
table.sort(collection, function(a, b) return a[sortKey] < b[sortKey] end)
end
local i = 1
local current = nil
return {
next = function()
for _ = i, #collection do
local e = collection[i]
if pred(e, i) then
current = e._name
i = i + 1
return true
end
i = i + 1
end
current = nil
return false
end,
current = function()
return current
end,
}
end
function Iterator.equipmentById(context, n)
local from = numberKey('from', context, n, 1)
local to = numberKey('to', context, n, 500)
return Iterator.equipmentBy(context, n, function(_, i)
-- use _id?
return i >= from and i <= to
end)
end
function Iterator.equipmentByType(context, n)
local type = numberKey('type', context, n)
return Iterator.equipmentBy(context, n, function(e)
return e._type == type
end)
end
function Iterator.equipmentByTypeAndIcon(context, n)
local type = numberKey('type', context, n)
local icon = numberKey('icon', context, n)
return Iterator.equipmentBy(context, n, function(e)
return e._type == type and e._icon == icon
end)
end
function Iterator.test()
function testIterator(name, args)
local iterator = Iterator[name](args)
mw.log(name)
while iterator.next() do
mw.log(' ' .. iterator.current())
end
mw.log()
end
testIterator('equipmentById', { from = '11', to = '20' })
testIterator('equipmentByType', { type = '2' })
testIterator('equipmentByTypeAndIcon', { type = '1', icon = '16' })
testIterator('equipmentBy', { pred = 'is_large_caliber_main_gun', sort = '_type' })
end
-- p.test()
return Iterator