LuaQ is a reusable Lua library for working with iterables through a fluent Enumerable API.
See the LuaQ Wiki for detailed guidance on how to use the library and its functions. The Wiki pages were generated by an AI assistant from the LuaQ source code.
Here is a small example that filters a list of drops, sorts the valuable ones, and prints their names. The item values are illustrative, not live prices.
local LuaQ = require("src.enumerable")
local drops = {
{ item = "Onyx", value = 100000 },
{ item = "Soulbound lantern", value = 42000000 },
{ item = "Spirit weed seed", value = 5000 },
{ item = "Bow of the Last Guardian", value = 65000000 },
}
local valuableDrops = LuaQ.from(drops)
:where(function(drop) return drop.value >= 1000000 end)
:sortByDescending(function(drop) return drop.value end)
:map(function(drop) return drop.item end)
valuableDrops:foreach(function(item)
print(item)
end)Each function uses the sequence produced by the function before it: where
keeps drops worth at least 1,000,000 GP, sortByDescending puts the most
valuable first, and map picks each item's name. foreach then prints those
names.
This example uses a flat list of drops to find the total value of valuable drops for each boss, then sorts the bosses by that total.
local LuaQ = require("src.enumerable")
local drops = {
{ boss = "Zamorak", item = "Chaos Roar ability codex", value = 18000000 },
{ boss = "Zamorak", item = "Onyx", value = 100000 },
{ boss = "Rasial", item = "Soulbound lantern", value = 42000000 },
{ boss = "Zamorak", item = "Bow of the Last Guardian", value = 65000000 },
{ boss = "Rasial", item = "Spirit weed seed", value = 5000 },
}
local bossTotals = LuaQ.from(drops)
:where(function(drop) return drop.value >= 1000000 end)
:groupByResult(
function(drop) return drop.boss end,
function(drop) return drop.value end,
function(boss, values)
return {
boss = boss,
valuableDropCount = #values,
totalValue = LuaQ.sum(values),
}
end
)
:sortByDescending(function(summary) return summary.totalValue end)
bossTotals:foreach(function(summary)
print(("%s: %d valuable drops, %d GP total"):format(
summary.boss,
summary.valuableDropCount,
summary.totalValue
))
end)Here, where filters out low-value drops. groupByResult collects the
remaining values by boss and creates a summary for each group, and
sortByDescending puts the boss with the highest total first.
For more examples and details about LuaQ's functions, visit the LuaQ Wiki.
LuaQ uses deferred execution: creating the query does not sift through the
drops right away. It starts processing when you ask for the results, such
as by calling foreach or toTable. This makes it easy to combine steps
without writing a series of loops and building temporary tables yourself.
Run the test suite from the repository root with Lua:
lua tests\enumerable_test.lua