Skip to content
Dennis Corvers edited this page Oct 23, 2026 · 8 revisions

LuaQ

LuaQ is a reusable Lua library for working with iterables through a fluent Enumerable API. It provides chainable filtering, projection, set operations, sorting, grouping, queries, and collection conversions.

Documentation

  • Getting Started — load LuaQ, create an Enumerable, and run the tests.
  • Functions — reference pages for operators, queries, iteration, and conversion.

Example: summarizing boss drops

A loot tracker can record drops from several bosses. To report valuable drops by boss, ordinary Lua needs to filter the records, maintain one accumulator per boss, collect the summaries, and sort them. Assume drops is a list of records with boss, item, and value fields, where value is in GP:

local minimumValue = 1000000
local byBoss = {}

for _, drop in ipairs(drops) do
    if drop.value >= minimumValue then
        local summary = byBoss[drop.boss]
        if summary == nil then
            summary = { boss = drop.boss, count = 0, totalValue = 0 }
            byBoss[drop.boss] = summary
        end
        summary.count = summary.count + 1
        summary.totalValue = summary.totalValue + drop.value
    end
end

local summaries = {}
for _, summary in pairs(byBoss) do
    table.insert(summaries, summary)
end
table.sort(summaries, function(a, b)
    return a.totalValue > b.totalValue
end)

With LuaQ, the same filter, grouping, and sort can be expressed as one query:

local minimumValue = 1000000
local summaries = LuaQ.from(drops)
    :where(function(drop) return drop.value >= minimumValue end)
    :groupByResult(
        function(drop) return drop.boss end,
        function(drop) return drop.value end,
        function(boss, values)
            return {
                boss = boss,
                count = #values,
                totalValue = LuaQ.sum(values),
            }
        end
    )
    :sortByDescending(function(summary) return summary.totalValue end)
    :toTable()

Both versions produce one summary per boss, containing the number and total value of drops worth at least 1,000,000 GP, with the highest total first. LuaQ keeps the filtering, grouping, and ordering steps together instead of requiring separate accumulator and sorting code.

Source

The library source is src/enumerable.lua. The test suite is in tests.

Clone this wiki locally