Skip to content

Getting Started

Dennis Corvers edited this page Oct 23, 2026 · 2 revisions

Getting Started

Install

Clone or download the LuaQ repository. Keep the src directory in a location available to Lua's package.path.

From the repository root, load the library with:

local LuaQ = require("src.enumerable")

The module returns the Enumerable API table. LuaQ.from(data) (an alias of LuaQ.create(data)) wraps a supported iterable in an Enumerable.

Example: first query

local LuaQ = require("src.enumerable")

local result = LuaQ.from({
    { name = "Ada", level = 92, role = "mage" },
    { name = "Bryn", level = 78, role = "ranger" },
    { name = "Cato", level = 85, role = "mage" },
})
    :where(function(player) return player.level >= 80 end)
    :map(function(player) return player.name end)
    :toTable()

-- result is { "Ada", "Cato" }

Chainable operators return an Enumerable. Use terminal methods such as toTable, count, first, or sum to get a result.

Creating from custom iterators

For a source that LuaQ cannot recognize automatically, use createFrom. The iterator factory must return the iterator function, its state, and its initial control value, just like Lua's generic-for iterator tuple:

local source = { "a", "b", "c" }

local values = LuaQ.createFrom(source, function(items)
    return ipairs(items)
end)

Enumerable.new(nextFunc, index, contextFactory) is available for constructing an Enumerable directly. The context factory should return a fresh iterator state for each enumeration.

Iterating

Use LuaQ's iterator methods for portable generic-for loops:

for index, value in values:ipairs() do
    print(index, value)
end

values:pairs() also returns the iterator tuple. Whether plain ipairs(values) or pairs(values) honors metamethods depends on the Lua runtime; prefer the explicit methods when portability matters.

Tests

Run the tests from the repository root:

lua tests\enumerable_test.lua

Clone this wiki locally