Module:FunList/Test

From Melvor Idle
< Module:FunList
Revision as of 20:09, 26 July 2024 by Ricewind (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

local Debug = require('Module:Debug')
local TableIterator = require('Module:FunList/Sandbox')

local mainTable = {
    [1] = {
        id = 1,
        name = "Item One",
        details = {
            description = "This is the first item.",
            price = 10.99
        }
    },
    [2] = {
        id = 2,
        name = "Item Two",
        details = {
            description = "This is the second item.",
            price = 20.49
        }
    },
    [3] = {
        id = 3,
        name = "Item Three",
        details = {
            description = "This is the third item.",
            price = 15.75
        }
    }
} 
function p.test()
    local tbl = {}
    for i = 1, 10 do
        tbl[i] = string.char(96 + i) -- 96 is the ASCII value for 'a' minus 1
    end
    --tbl['#'] = '##'
    table.insert(tbl, 'a')
    table.insert(tbl, 'd')
    table.insert(tbl, 'f')

    local enum = TableIterator.new(tbl)
    
    local result = TableIterator.new(mainTable)
        :where(function(x) return x.id % 2 ~= 0 end)
        :flatMap(function(x) return x.details end)
        :where(function(x) return tonumber(x) ~= nil end)
        :sum()
        
    Debug.log(result)
    Debug.log('@@end@@')
end


return p