Anonymous

Module:FunList: Difference between revisions

From Melvor Idle
no edit summary
No edit summary
No edit summary
Line 150: Line 150:
local num = tonumber(v)
local num = tonumber(v)
if num == nil then error('Value is NaN') end
if num == nil then error('Value is NaN') end
if v > h then h = v end
if num > h then h = num end
end
end
return h
return h
Line 161: Line 161:
local num = tonumber(v)
local num = tonumber(v)
if num == nil then error('Value is NaN') end
if num == nil then error('Value is NaN') end
if v < l then l = v end
if num < l then l = num end
end
end
return l
return l
end
function funlist:sum()
local total = 0
for _, v in pairs(self.mytable) do
local num = tonumber(v)
if num == nil then error('Value is NaN') end
total = total + num
end
return total
end
end


Line 210: Line 220:
table.sort(self.mytable, sortFunc)
table.sort(self.mytable, sortFunc)
return self
return self
end
-- Projects each element of a sequence into a new form.
function funlist:select(selector)
assert(selector)
local result = {}
    for _, v in pairs(self.mytable) do
    local val = selector(v)
    assert(val)
        table.insert(result, selector(v))
    end
    self.mytable = result
    return self
end
-- Projects each element of a sequence into a new form and flattens the result.
function funlist:selectMany(selector)
assert(selector)
local result = {}
for _, item in pairs(self.mytable) do
local tbl = selector(item)
-- Resulting object must be a table here!
assert(typeof(tbl) == 'table')
for _, value in pairs(tbl) do
table.insert(result, value)
end
end
end
-- Filters a sequence of values based on a predicate.
function funlist:where(predicate)
assert(predicate)
    local result = {}
    for k, v in pairs(tbl) do
        if predicate(v) then
        result[k] = v
        end
    end
    self.mytable = result
    return self
end
function funlist:toDictionary(keySelector, valueSelector)
assert(keySelector)
assert(valueSelector)
local dict = {}
for _, item in pairs(self.mytable) do
local key = keyselector(item)
local val = valueSelector(item)
dict[key] = val
end
-- Create and return a new FL so we can chain more functions if needed.
local newFL = funlist.new(dict)
return newFL
end
function funlist:toLookup()
end
function funlist:getTable()
return self.mytable
end
end


2,875

edits