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

Enumerable:unique

Returns the first value for each distinct key in an Enumerable.

Syntax

distinct = source:unique(keySelector)

Parameters

Parameter Type Description
keySelector function(value, sourceKey) (optional) Selects the key used to identify duplicate values.

Returns

A deferred Enumerable with only the first value for each key, in source iteration order.

Remarks

If no selector is supplied, the value itself is used as the key. distinct is an alias. Keys are stored in a Lua table and therefore must not be nil.

Example

local players = {
    { name = "Ada", role = "mage" },
    { name = "Bryn", role = "ranger" },
    { name = "Cato", role = "mage" },
}

local firstByRole = LuaQ.from(players)
    :unique(function(player) return player.role end)
    :map(function(player) return player.name end)
    :toTable()
-- { "Ada", "Bryn" }

See also

Difference · Union · Intersect · Functions

Clone this wiki locally