Module:FunList/Test: Difference between revisions

From Melvor Idle
(Created page with "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 } },...")
 
No edit summary
 
Line 42: Line 42:
     local enum = TableIterator.new(tbl)
     local enum = TableIterator.new(tbl)
      
      
     local result = enum
     local result = TableIterator.new(mainTable)
         :map(function (x) return 'Result: ' .. x end)
         :where(function(x) return x.id % 2 ~= 0 end)
        :zip({1,2,3})
         :flatMap(function(x) return x.details end)
         :flatMap(function(x) return x end)
         :where(function(x) return tonumber(x) ~= nil end)
         :toDictionary(function(key, i) return key end, function(v, i) return i end)
        :sum()
          
          
     Debug.log(result)
     Debug.log(result)

Latest revision as of 20:09, 26 July 2024

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