Repository navigation
Getting Started
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.
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.
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.
Use LuaQ's iterator methods for portable generic-for loops:
for index, value in values:ipairs() do
print(index, value)
endvalues: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.
Run the tests from the repository root:
lua tests\enumerable_test.lua- Home
- Getting Started
-
Functions
- aggregate
- all
- any
- concat
- contains
- count
- create
- createFrom
- deepCopy
- difference
- elementAt
- elementAtOrDefault
- first
- firstOrDefault
- flatMap
- foreach
- groupBy
- groupByResult
- indexOf
- intersect
- ipairs
- last
- lastOrDefault
- map
- max
- min
- pairs
- sequenceEquals
- single
- singleOrDefault
- sort
- sortBy
- sortByDescending
- sortDescending
- sum
- take
- takeWhile
- thenBy
- thenByDecending
- toLookup
- toMap
- toSet
- toTable
- union
- unique
- where
- zip
- Aliases