nvim_runtime_lua

vim.Iter

LuaLS fallback surface for the richer iterator annotations in vim.iter. EmmyLua reads the precise generics from runtime/lua/vim/iter.lua; LuaLS uses these broader shapes for downstream type-checking.

Methods53

function Iter._next() -> ...V1
function Iter.__call(self) -> any
function Iter.filter(f: fun(v: V1, ...: ...V) -> boolean) -> vim.Iter<V1, ...V>

Filters an iterator pipeline.

Example:

local bufs = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded)
Overloads
function Iter.filter(self: vim.IterArray<V1, ...V>, f: fun(v: V1, ...: ...V) -> boolean) -> vim.IterArray<V1, ...V>
Parameters
ffun(v: V1, ...: ...V) -> boolean

Takes all values returned from the previous stage in the pipeline and returns false or nil if the current iterator element should be removed.

Returns
vim.Iter<V1, ...V>
function Iter.unique(key: fun(v: V1, ...: ...V) -> any | nil) -> vim.Iter<V1, ...V>

Removes duplicate values from an iterator pipeline.

Only the first occurrence of each value is kept.

Accepts an optional key argument, which if provided is called for each value in the iterator to compute a hash key for uniqueness comparison. This is useful for deduplicating table values or complex objects. If key returns nil for a value, that value will be considered unique, even if multiple values return nil.

If a function-based iterator returns multiple arguments, uniqueness is checked based on the first return value. To change this behavior, specify key.

Examples:

vim.iter({ 1, 2, 2, 3, 2 }):unique():totable()
-- { 1, 2, 3 }

vim.iter({ {id=1}, {id=2}, {id=1} })
  :unique(function(x)
    return x.id
  end)
  :totable()
-- { {id=1}, {id=2} }
Overloads
function Iter.unique(self: vim.IterArray<V1, ...V>, key: fun(v: V1, ...: ...V) -> any) -> vim.IterArray<V1, ...V>
Parameters
keyfun(v: V1, ...: ...V) -> any | nil

Optional hash function to determine uniqueness of values.

Returns
vim.Iter<V1, ...V>
See:

|vim.list.unique()|

@since 14

function Iter.flatten(depth: nil | integer) -> any

@nodoc

function Iter.map<K2, V2>(f: fun(v: V1, ...: ...V) -> ...K2) -> vim.Iter<K2, ...V2>

Maps the items of an iterator pipeline to the values returned by f.

If the map function returns nil, the value is filtered from the iterator.

Example:

local it = vim.iter({ 1, 2, 3, 4 }):map(function(v)
  if v % 2 == 0 then
    return v * 3
  end
end)
it:totable()
-- { 6, 12 }
Overloads
function Iter.map(self: vim.IterArray<V1, ...V>, f: fun(v: V1, ...: ...V) -> ...K1) -> vim.IterArray<K1, ...K>
Parameters
ffun(v: V1, ...: ...V) -> ...K2

Mapping function. Takes all values returned from the previous stage in the pipeline as arguments and returns one or more new values, which are used in the next pipeline stage. Nil return values are filtered from the output.

Returns
vim.Iter<K2, ...V2>

@since 12

function Iter.next() -> any
function Iter.each(f: fun(v: V1, ...: ...V) -> nil) -> nil

Calls a function once for each item in the pipeline, draining the iterator.

For functions with side effects. To modify the values in the iterator, use |Iter:map()|.

Parameters
ffun(v: V1, ...: ...V) -> nil

Function to execute for each item in the pipeline. Takes all of the values returned by the previous stage in the pipeline as arguments.

Returns
nil

@since 12

function Iter.totable() -> any[]

Collect the iterator into a table.

The resulting table depends on the initial source in the iterator pipeline. Array-like tables and function iterators will be collected into an array-like table. If multiple values are returned from the final stage in the iterator pipeline, each value will be included in a table.

Examples:

vim.iter(string.gmatch('100 20 50', '%d+')):map(tonumber):totable()
-- { 100, 20, 50 }

vim.iter({ 1, 2, 3 }):map(function(v) return v, 2 * v end):totable()
-- { { 1, 2 }, { 2, 4 }, { 3, 6 } }

vim.iter({ a = 1, b = 2, c = 3 }):filter(function(k, v) return v % 2 ~= 0 end):totable()
-- { { 'a', 1 }, { 'c', 3 } }

The generated table is an array-like table with consecutive, numeric indices. To create a map-like table with arbitrary keys, use |Iter:fold()|.

Overloads
function Iter.totable(self: any) -> T[]
function Iter.totable(self: vim.Iter<V1, V2, ...V>) -> (V1, V2, ...V)[]

@since 12

function Iter.join(delim: string) -> string

Collect the iterator into a delimited string.

Each element in the iterator is joined into a string separated by {delim}.

Consumes the iterator.

Parameters
delimstring

Delimiter

Returns
string

@since 12

function Iter.fold<A>(init: A, f: fun(acc: A, v: V1, ...: ...V) -> A) -> A

Folds ("reduces") an iterator into a single value. Iter:reduce()

Examples:

-- Create a new table with only even values
vim.iter({ a = 1, b = 2, c = 3, d = 4 })
  :filter(function(k, v) return v % 2 == 0 end)
  :fold({}, function(acc, k, v)
    acc[k] = v
    return acc
  end) --> { b = 2, d = 4 }

-- Get the "maximum" item of an iterable.
vim.iter({ -99, -4, 3, 42, 0, 0, 7 })
  :fold({}, function(acc, v)
    acc.max = math.max(v, acc.max or v)
    return acc
  end) --> { max = 42 }
Parameters
initA

Initial value of the accumulator.

ffun(acc: A, v: V1, ...: ...V) -> A

Accumulation function.

Returns
A

@since 12

function Iter.next() -> (V1 | nil, ...V)

@since 12

function Iter.rev() -> any

@nodoc

function Iter.peek() -> (V1 | nil, ...V)

Gets the next value from the iterator without consuming it.

The value returned by |Iter:peek()| will be returned again by the next call to |Iter:next()|.

Example:


local it = vim.iter({ 3, 6, 9, 12 })
it:peek()
-- 3
it:peek()
-- 3
it:next()
-- 3

@since 12

function Iter.find(f: fun(v: V1, ...: ...V) -> boolean | any) -> (V1 | nil, ...V)

Find the first value in the iterator that satisfies the given predicate.

Advances the iterator. Returns nil and drains the iterator if no value is found.

Examples:


local it = vim.iter({ 3, 6, 9, 12 })
it:find(12)
-- 12

local it = vim.iter({ 3, 6, 9, 12 })
it:find(20)
-- nil

local it = vim.iter({ 3, 6, 9, 12 })
it:find(function(v) return v % 4 == 0 end)
-- 12

Overloads
function Iter.find(self: vim.IterArray<V1, ...V>, f: V1 | fun(v: V1, ...: ...V) -> boolean) -> ...V1 | nil

@since 12

function Iter.rfind(f: any) -> any

@nodoc

function Iter.take(n: integer | fun(v: V1, ...: ...V) -> boolean) -> vim.Iter<V1, ...V>

Transforms an iterator to yield only the first n values, or all values satisfying a predicate.

Example:

local it = vim.iter({ 1, 2, 3, 4 }):take(2)
it:next()
-- 1
it:next()
-- 2
it:next()
-- nil

local function pred(x) return x < 2 end
local it2 = vim.iter({ 1, 2, 3, 4 }):take(pred)
it2:next()
-- 1
it2:next()
-- nil
Overloads
function Iter.take(self: vim.IterArray<V1, ...V>, n: integer | fun(v: V1, ...: ...V) -> boolean) -> vim.IterArray<V1, ...V>
Parameters
ninteger | fun(v: V1, ...: ...V) -> boolean

Number of values to take or a predicate.

Returns
vim.Iter<V1, ...V>

@since 12

function Iter.pop() -> any

@nodoc

function Iter.rpeek() -> any

@nodoc

function Iter.skip(n: integer | fun(v: V1, ...: ...V) -> boolean) -> vim.Iter<V1, ...V>

Skips n values of an iterator pipeline, or skips values while a predicate returns |lua-truthy|.

When a predicate is used, skipping stops at the first value for which the predicate returns non-truthy. That value is not consumed and will be returned by the next call to |Iter:next()|

Example:


local it = vim.iter({ 3, 6, 9, 12 }):skip(2)
it:next()
-- 9

local function pred(x) return x < 10 end
local it2 = vim.iter({ 3, 6, 9, 12 }):skip(pred)
it2:next()
-- 12
Overloads
function Iter.skip(self: vim.IterArray<V1, ...V>, n: integer | fun(v: V1, ...: ...V) -> boolean) -> vim.IterArray<V1, ...V>
Parameters
ninteger | fun(v: V1, ...: ...V) -> boolean

Number of values to skip or a predicate.

Returns
vim.Iter<V1, ...V>

@since 12

function Iter.rskip(n: integer) -> any

@nodoc

function Iter.nth(n: integer) -> (V1 | nil, ...V)

Gets the nth value of an iterator (and advances to it).

If n is negative, offsets from the end of a |list-iterator|.

Example:

local it = vim.iter({ 3, 6, 9, 12 })
it:nth(2)
-- 6
it:nth(2)
-- 12

local it2 = vim.iter({ 3, 6, 9, 12 })
it2:nth(-2)
-- 9
it2:nth(-2)
-- 3
Parameters
ninteger

Index of the value to return. May be negative if the source is a |list-iterator|.

Returns
V1 | nil
...V

@since 12

function Iter.slice(first: integer, last: integer) -> any

@nodoc

function Iter.any(pred: fun(v: V1, ...: ...V) -> boolean) -> false

Returns true if any of the items in the iterator match the given predicate.

Parameters
predfun(v: V1, ...: ...V) -> boolean

Predicate function. Takes all values returned from the previous stage in the pipeline as arguments and returns true if the predicate matches.

Returns
false

@since 12

function Iter.all(pred: fun(v: V1, ...: ...V) -> boolean) -> true

Returns true if all items in the iterator match the given predicate.

Parameters
predfun(v: V1, ...: ...V) -> boolean

Predicate function. Takes all values returned from the previous stage in the pipeline as arguments and returns true if the predicate matches.

Returns
true

@since 12

function Iter.last() -> (V1 | nil, ...V)

Drains the iterator and returns the last item.

Example:


local it = vim.iter(vim.gsplit('abcdefg', ''))
it:last()
-- 'g'

local it = vim.iter({ 3, 6, 9, 12, 15 })
it:last()
-- 15

See:

|IterArray:rpeek()|

@since 12

function Iter.enumerate() -> vim.Iter<integer, V1, ...V>

Yields the item index (count) and value for each item of an iterator pipeline.

For list tables, this is more efficient:

vim.iter(ipairs(t))

instead of:

vim.iter(t):enumerate()

Example:


local it = vim.iter(vim.gsplit('abc', '')):enumerate()
it:next()
-- 1	'a'
it:next()
-- 2	'b'
it:next()
-- 3	'c'

Overloads
function Iter.enumerate(self: vim.IterArray<V1, ...V>) -> vim.IterArray<integer, V1, ...V>

@since 12

function Iter.count() -> integer

Drains the iterator, counting the number of iterations and returning it.

@since 15

function Iter.all(self: any, pred: fun(...) -> boolean) -> boolean
function Iter.any(self: any, pred: fun(...) -> boolean) -> boolean
function Iter.count(self: any) -> integer
function Iter.each(self: any, f: fun(...) -> nil) -> nil
function Iter.enumerate(self: any) -> any
function Iter.filter(self: any, f: fun(...) -> boolean) -> any
function Iter.find(self: any, f: any) -> any
function Iter.flatten(self: any, depth: nil | integer) -> any
function Iter.fold(self: any, init: any, f: fun(acc: any, ...) -> any) -> any
function Iter.join(self: any, delim: string) -> string
function Iter.last(self: any) -> any
function Iter.map(self: any, f: fun(...) -> unknown) -> any
function Iter.next(self: any) -> any
function Iter.nth(self: any, n: integer) -> any
function Iter.peek(self: any) -> any
function Iter.pop(self: any) -> any
function Iter.rev(self: any) -> any
function Iter.rfind(self: any, f: any) -> any
function Iter.rpeek(self: any) -> any
function Iter.rskip(self: any, n: integer) -> any
function Iter.skip(self: any, n: integer | fun(...) -> boolean) -> any
function Iter.slice(self: any, first: integer, last: integer) -> any
function Iter.take(self: any, n: integer | fun(...) -> boolean) -> any
function Iter.totable(self: any) -> table
function Iter.unique(self: any, key: fun(...) -> any | nil) -> any

Fields1

Iter.__index : vim.Iter