Skip to content

TakeWhile

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

Enumerable:takeWhile

Returns values from the beginning of an Enumerable while a predicate accepts each value. Enumeration stops at the first value the predicate rejects.

Syntax

prefix = source:takeWhile(predicate)

Parameters

Parameter Type Description
predicate function(value, position) Returns true to keep the value and continue. Position is 1-based.

Returns

A deferred Enumerable containing the initial consecutive values accepted by the predicate.

Remarks

The predicate must return exactly true to continue. Values after the first rejected value are not examined.

Example

local priceRange = LuaQ.from({
    { item = "Rune platebody", price = 38000 },
    { item = "Dragon boots", price = 42000 },
    { item = "Noxious scythe", price = 240000000 },
})
    :takeWhile(function(offer) return offer.price < 50000 end)
    :toTable()
-- Takes affordable offers up to the first expensive item

See also

Take · Where · Functions

Clone this wiki locally