2024-02-15 10:16:04 -07:00
|
|
|
--- @brief
|
2023-04-24 18:57:40 -07:00
|
|
|
---
|
2024-03-08 05:25:18 -07:00
|
|
|
--- [vim.iter()]() is an interface for [iterable]s: it wraps a table or function argument into an
|
|
|
|
--- [Iter]() object with methods (such as [Iter:filter()] and [Iter:map()]) that transform the
|
2023-11-25 07:35:31 -07:00
|
|
|
--- underlying source data. These methods can be chained to create iterator "pipelines": the output
|
|
|
|
--- of each pipeline stage is input to the next stage. The first stage depends on the type passed to
|
|
|
|
--- `vim.iter()`:
|
2023-04-24 18:57:40 -07:00
|
|
|
---
|
2024-09-01 13:01:24 -07:00
|
|
|
--- - Lists or arrays (|lua-list|) yield only the value of each element.
|
|
|
|
--- - Holes (nil values) are allowed (but discarded).
|
|
|
|
--- - Use pairs() to treat array/list tables as dicts (preserve holes and non-contiguous integer
|
|
|
|
--- keys): `vim.iter(pairs(…))`.
|
2023-11-25 07:35:31 -07:00
|
|
|
--- - Use |Iter:enumerate()| to also pass the index to the next stage.
|
2024-09-01 13:01:24 -07:00
|
|
|
--- - Or initialize with ipairs(): `vim.iter(ipairs(…))`.
|
2023-11-25 07:35:31 -07:00
|
|
|
--- - Non-list tables (|lua-dict|) yield both the key and value of each element.
|
|
|
|
--- - Function |iterator|s yield all values returned by the underlying function.
|
|
|
|
--- - Tables with a |__call()| metamethod are treated as function iterators.
|
2023-04-24 18:57:40 -07:00
|
|
|
---
|
2023-11-25 07:35:31 -07:00
|
|
|
--- The iterator pipeline terminates when the underlying |iterable| is exhausted (for function
|
|
|
|
--- iterators this means it returned nil).
|
|
|
|
---
|
|
|
|
--- Note: `vim.iter()` scans table input to decide if it is a list or a dict; to avoid this cost you
|
|
|
|
--- can wrap the table with an iterator e.g. `vim.iter(ipairs({…}))`, but that precludes the use of
|
|
|
|
--- |list-iterator| operations such as |Iter:rev()|).
|
2023-08-09 13:41:45 -07:00
|
|
|
---
|
2023-04-24 18:57:40 -07:00
|
|
|
--- Examples:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- local it = vim.iter({ 1, 2, 3, 4, 5 })
|
|
|
|
--- it:map(function(v)
|
|
|
|
--- return v * 3
|
|
|
|
--- end)
|
|
|
|
--- it:rev()
|
|
|
|
--- it:skip(2)
|
|
|
|
--- it:totable()
|
|
|
|
--- -- { 9, 6, 3 }
|
|
|
|
---
|
|
|
|
--- -- ipairs() is a function iterator which returns both the index (i) and the value (v)
|
|
|
|
--- vim.iter(ipairs({ 1, 2, 3, 4, 5 })):map(function(i, v)
|
|
|
|
--- if i > 2 then return v end
|
|
|
|
--- end):totable()
|
|
|
|
--- -- { 3, 4, 5 }
|
|
|
|
---
|
|
|
|
--- local it = vim.iter(vim.gsplit('1,2,3,4,5', ','))
|
|
|
|
--- it:map(function(s) return tonumber(s) end)
|
|
|
|
--- for i, d in it:enumerate() do
|
|
|
|
--- print(string.format("Column %d is %d", i, d))
|
|
|
|
--- end
|
|
|
|
--- -- Column 1 is 1
|
|
|
|
--- -- Column 2 is 2
|
|
|
|
--- -- Column 3 is 3
|
|
|
|
--- -- Column 4 is 4
|
|
|
|
--- -- Column 5 is 5
|
|
|
|
---
|
|
|
|
--- vim.iter({ a = 1, b = 2, c = 3, z = 26 }):any(function(k, v)
|
|
|
|
--- return k == 'z'
|
|
|
|
--- end)
|
|
|
|
--- -- true
|
|
|
|
---
|
|
|
|
--- local rb = vim.ringbuf(3)
|
|
|
|
--- rb:push("a")
|
|
|
|
--- rb:push("b")
|
|
|
|
--- vim.iter(rb):totable()
|
|
|
|
--- -- { "a", "b" }
|
|
|
|
--- ```
|
2023-04-24 18:57:40 -07:00
|
|
|
|
2024-03-06 05:15:25 -07:00
|
|
|
--- LuaLS is bad at generics which this module mostly deals with
|
|
|
|
--- @diagnostic disable:no-unknown
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@nodoc
|
2023-06-03 00:18:05 -07:00
|
|
|
---@class IterMod
|
|
|
|
---@operator call:Iter
|
2024-02-15 10:16:04 -07:00
|
|
|
|
2023-04-24 18:57:40 -07:00
|
|
|
local M = {}
|
2023-04-17 11:54:19 -07:00
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
---@nodoc
|
2023-04-17 11:54:19 -07:00
|
|
|
---@class Iter
|
|
|
|
local Iter = {}
|
|
|
|
Iter.__index = Iter
|
|
|
|
Iter.__call = function(self)
|
|
|
|
return self:next()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Special case implementations for iterators on list tables.
|
2024-02-27 08:20:32 -07:00
|
|
|
---@nodoc
|
2024-05-17 12:17:25 -07:00
|
|
|
---@class ArrayIter : Iter
|
2023-04-28 10:37:53 -07:00
|
|
|
---@field _table table Underlying table data
|
|
|
|
---@field _head number Index to the front of a table iterator
|
2023-06-03 03:06:10 -07:00
|
|
|
---@field _tail number Index to the end of a table iterator (exclusive)
|
2024-05-17 12:17:25 -07:00
|
|
|
local ArrayIter = {}
|
|
|
|
ArrayIter.__index = setmetatable(ArrayIter, Iter)
|
|
|
|
ArrayIter.__call = function(self)
|
2023-04-17 11:54:19 -07:00
|
|
|
return self:next()
|
|
|
|
end
|
|
|
|
|
2023-04-21 15:13:39 -07:00
|
|
|
--- Packed tables use this as their metatable
|
|
|
|
local packedmt = {}
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
local function unpack(t)
|
2023-04-28 10:37:53 -07:00
|
|
|
if type(t) == 'table' and getmetatable(t) == packedmt then
|
2023-04-21 15:13:39 -07:00
|
|
|
return _G.unpack(t, 1, t.n)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
local function pack(...)
|
2023-04-19 05:45:56 -07:00
|
|
|
local n = select('#', ...)
|
|
|
|
if n > 1 then
|
2023-04-21 15:13:39 -07:00
|
|
|
return setmetatable({ n = n, ... }, packedmt)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
|
2023-04-21 15:13:39 -07:00
|
|
|
local function sanitize(t)
|
2023-04-28 10:37:53 -07:00
|
|
|
if type(t) == 'table' and getmetatable(t) == packedmt then
|
2024-05-17 12:17:25 -07:00
|
|
|
-- Remove length tag and metatable
|
2023-04-21 15:13:39 -07:00
|
|
|
t.n = nil
|
2024-05-17 12:17:25 -07:00
|
|
|
setmetatable(t, nil)
|
2023-04-21 15:13:39 -07:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
--- Flattens a single array-like table. Errors if it attempts to flatten a
|
2024-01-10 19:57:51 -07:00
|
|
|
--- dict-like table
|
2024-05-17 12:17:25 -07:00
|
|
|
---@param t table table which should be flattened
|
2024-01-10 19:57:51 -07:00
|
|
|
---@param max_depth number depth to which the table should be flattened
|
|
|
|
---@param depth number current iteration depth
|
|
|
|
---@param result table output table that contains flattened result
|
|
|
|
---@return table|nil flattened table if it can be flattened, otherwise nil
|
2024-05-17 12:17:25 -07:00
|
|
|
local function flatten(t, max_depth, depth, result)
|
|
|
|
if depth < max_depth and type(t) == 'table' then
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if type(k) ~= 'number' or k <= 0 or math.floor(k) ~= k then
|
2024-01-10 19:57:51 -07:00
|
|
|
-- short-circuit: this is not a list like table
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
if flatten(v, max_depth, depth + 1, result) == nil then
|
2024-01-10 19:57:51 -07:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
2024-05-17 12:17:25 -07:00
|
|
|
elseif t ~= nil then
|
|
|
|
result[#result + 1] = t
|
2024-01-10 19:57:51 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2023-04-28 10:37:53 -07:00
|
|
|
--- Determine if the current iterator stage should continue.
|
|
|
|
---
|
|
|
|
--- If any arguments are passed to this function, then return those arguments
|
|
|
|
--- and stop the current iterator stage. Otherwise, return true to signal that
|
|
|
|
--- the current stage should continue.
|
|
|
|
---
|
|
|
|
---@param ... any Function arguments.
|
|
|
|
---@return boolean True if the iterator stage should continue, false otherwise
|
|
|
|
---@return any Function arguments.
|
|
|
|
local function continue(...)
|
2023-08-09 13:41:45 -07:00
|
|
|
if select(1, ...) ~= nil then
|
2023-04-28 10:37:53 -07:00
|
|
|
return false, ...
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- If no input arguments are given return false, indicating the current
|
|
|
|
--- iterator stage should stop. Otherwise, apply the arguments to the function
|
|
|
|
--- f. If that function returns no values, the current iterator stage continues.
|
|
|
|
--- Otherwise, those values are returned.
|
|
|
|
---
|
|
|
|
---@param f function Function to call with the given arguments
|
|
|
|
---@param ... any Arguments to apply to f
|
|
|
|
---@return boolean True if the iterator pipeline should continue, false otherwise
|
|
|
|
---@return any Return values of f
|
|
|
|
local function apply(f, ...)
|
2023-08-09 13:41:45 -07:00
|
|
|
if select(1, ...) ~= nil then
|
2023-04-28 10:37:53 -07:00
|
|
|
return continue(f(...))
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Filters an iterator pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- local bufs = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded)
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-01-18 06:01:57 -07:00
|
|
|
---@param f fun(...):boolean Takes all values returned from the previous stage
|
2024-01-09 10:36:46 -07:00
|
|
|
--- in the pipeline and returns false or nil if the
|
|
|
|
--- current iterator element should be removed.
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:filter(f)
|
2023-04-28 10:37:53 -07:00
|
|
|
return self:map(function(...)
|
|
|
|
if f(...) then
|
|
|
|
return ...
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
2023-04-28 10:37:53 -07:00
|
|
|
end)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:filter(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
local n = self._head
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
local v = self._table[i]
|
|
|
|
if f(unpack(v)) then
|
|
|
|
self._table[n] = v
|
|
|
|
n = n + inc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self._tail = n
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2024-01-10 19:57:51 -07:00
|
|
|
--- Flattens a |list-iterator|, un-nesting nested values up to the given {depth}.
|
|
|
|
--- Errors if it attempts to flatten a dict-like value.
|
|
|
|
---
|
|
|
|
--- Examples:
|
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- vim.iter({ 1, { 2 }, { { 3 } } }):flatten():totable()
|
|
|
|
--- -- { 1, 2, { 3 } }
|
|
|
|
---
|
|
|
|
--- vim.iter({1, { { a = 2 } }, { 3 } }):flatten():totable()
|
|
|
|
--- -- { 1, { a = 2 }, 3 }
|
|
|
|
---
|
|
|
|
--- vim.iter({ 1, { { a = 2 } }, { 3 } }):flatten(math.huge):totable()
|
|
|
|
--- -- error: attempt to flatten a dict-like table
|
|
|
|
--- ```
|
|
|
|
---
|
|
|
|
---@param depth? number Depth to which |list-iterator| should be flattened
|
|
|
|
--- (defaults to 1)
|
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
---@diagnostic disable-next-line:unused-local
|
|
|
|
function Iter:flatten(depth) -- luacheck: no unused args
|
2024-05-17 12:17:25 -07:00
|
|
|
error('flatten() requires an array-like table')
|
2024-01-10 19:57:51 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:flatten(depth)
|
2024-01-10 19:57:51 -07:00
|
|
|
depth = depth or 1
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
local target = {}
|
|
|
|
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
local flattened = flatten(self._table[i], depth, 0, {})
|
|
|
|
|
|
|
|
-- exit early if we try to flatten a dict-like table
|
|
|
|
if flattened == nil then
|
2024-05-17 12:17:25 -07:00
|
|
|
error('flatten() requires an array-like table')
|
2024-01-10 19:57:51 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
for _, v in pairs(flattened) do
|
|
|
|
target[#target + 1] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self._head = 1
|
|
|
|
self._tail = #target + 1
|
|
|
|
self._table = target
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Maps the items of an iterator pipeline to the values returned by `f`.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- If the map function returns nil, the value is filtered from the iterator.
|
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- 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 }
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-06-01 00:18:59 -07:00
|
|
|
---@param f fun(...):...:any Mapping function. Takes all values returned from
|
2024-01-09 10:36:46 -07:00
|
|
|
--- 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.
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:map(f)
|
2023-04-28 10:37:53 -07:00
|
|
|
-- Implementation note: the reader may be forgiven for observing that this
|
|
|
|
-- function appears excessively convoluted. The problem to solve is that each
|
|
|
|
-- stage of the iterator pipeline can return any number of values, and the
|
|
|
|
-- number of values could even change per iteration. And the return values
|
|
|
|
-- must be checked to determine if the pipeline has ended, so we cannot
|
|
|
|
-- naively forward them along to the next stage.
|
|
|
|
--
|
|
|
|
-- A simple approach is to pack all of the return values into a table, check
|
|
|
|
-- for nil, then unpack the table for the next stage. However, packing and
|
|
|
|
-- unpacking tables is quite slow. There is no other way in Lua to handle an
|
|
|
|
-- unknown number of function return values than to simply forward those
|
|
|
|
-- values along to another function. Hence the intricate function passing you
|
|
|
|
-- see here.
|
|
|
|
|
|
|
|
local next = self.next
|
|
|
|
|
|
|
|
--- Drain values from the upstream iterator source until a value can be
|
|
|
|
--- returned.
|
|
|
|
---
|
|
|
|
--- This is a recursive function. The base case is when the first argument is
|
|
|
|
--- false, which indicates that the rest of the arguments should be returned
|
|
|
|
--- as the values for the current iteration stage.
|
|
|
|
---
|
|
|
|
---@param cont boolean If true, the current iterator stage should continue to
|
|
|
|
--- pull values from its upstream pipeline stage.
|
|
|
|
--- Otherwise, this stage is complete and returns the
|
|
|
|
--- values passed.
|
|
|
|
---@param ... any Values to return if cont is false.
|
|
|
|
---@return any
|
|
|
|
local function fn(cont, ...)
|
|
|
|
if cont then
|
|
|
|
return fn(apply(f, next(self)))
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
2023-04-28 10:37:53 -07:00
|
|
|
return ...
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2023-04-28 10:37:53 -07:00
|
|
|
self.next = function()
|
|
|
|
return fn(apply(f, next(self)))
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:map(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
local n = self._head
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
local v = pack(f(unpack(self._table[i])))
|
|
|
|
if v ~= nil then
|
|
|
|
self._table[n] = v
|
|
|
|
n = n + inc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self._tail = n
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Calls a function once for each item in the pipeline, draining the iterator.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2023-11-25 07:35:31 -07:00
|
|
|
--- For functions with side effects. To modify the values in the iterator, use |Iter:map()|.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-01-09 10:36:46 -07:00
|
|
|
---@param f fun(...) Function to execute for each item in the pipeline.
|
|
|
|
--- Takes all of the values returned by the previous stage
|
|
|
|
--- in the pipeline as arguments.
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:each(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local function fn(...)
|
2023-08-09 13:41:45 -07:00
|
|
|
if select(1, ...) ~= nil then
|
2023-04-17 11:54:19 -07:00
|
|
|
f(...)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
while fn(self:next()) do
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:each(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
f(unpack(self._table[i]))
|
|
|
|
end
|
|
|
|
self._head = self._tail
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Collect the iterator into a table.
|
|
|
|
---
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- The resulting table depends on the initial source in the iterator pipeline.
|
2024-05-17 12:17:25 -07:00
|
|
|
--- Array-like tables and function iterators will be collected into an array-like
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- table. If multiple values are returned from the final stage in the iterator
|
|
|
|
--- pipeline, each value will be included in a table.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Examples:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- 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()
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- -- { { 'a', 1 }, { 'c', 3 } }
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-05-17 12:17:25 -07:00
|
|
|
--- The generated table is an array-like table with consecutive, numeric indices.
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- To create a map-like table with arbitrary keys, use |Iter:fold()|.
|
|
|
|
---
|
|
|
|
---
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return table
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:totable()
|
2023-04-17 11:54:19 -07:00
|
|
|
local t = {}
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local args = pack(self:next())
|
|
|
|
if args == nil then
|
|
|
|
break
|
|
|
|
end
|
2023-04-19 05:45:56 -07:00
|
|
|
|
2023-04-21 15:13:39 -07:00
|
|
|
t[#t + 1] = sanitize(args)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:totable()
|
|
|
|
if self.next ~= ArrayIter.next or self._head >= self._tail then
|
2023-06-03 03:06:10 -07:00
|
|
|
return Iter.totable(self)
|
|
|
|
end
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
local needs_sanitize = getmetatable(self._table[self._head]) == packedmt
|
2023-06-03 03:06:10 -07:00
|
|
|
|
|
|
|
-- Reindex and sanitize.
|
|
|
|
local len = self._tail - self._head
|
|
|
|
|
|
|
|
if needs_sanitize then
|
|
|
|
for i = 1, len do
|
|
|
|
self._table[i] = sanitize(self._table[self._head - 1 + i])
|
2023-04-19 05:45:56 -07:00
|
|
|
end
|
2023-06-03 03:06:10 -07:00
|
|
|
else
|
|
|
|
for i = 1, len do
|
|
|
|
self._table[i] = self._table[self._head - 1 + i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = len + 1, table.maxn(self._table) do
|
|
|
|
self._table[i] = nil
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2023-06-03 03:06:10 -07:00
|
|
|
self._head = 1
|
|
|
|
self._tail = len + 1
|
|
|
|
|
|
|
|
return self._table
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2023-12-05 19:35:22 -07:00
|
|
|
--- Collect the iterator into a delimited string.
|
|
|
|
---
|
|
|
|
--- Each element in the iterator is joined into a string separated by {delim}.
|
|
|
|
---
|
|
|
|
--- Consumes the iterator.
|
|
|
|
---
|
|
|
|
--- @param delim string Delimiter
|
|
|
|
--- @return string
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:join(delim)
|
2023-12-05 19:35:22 -07:00
|
|
|
return table.concat(self:totable(), delim)
|
|
|
|
end
|
|
|
|
|
2024-04-30 04:30:21 -07:00
|
|
|
--- Folds ("reduces") an iterator into a single value. [Iter:reduce()]()
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- Examples:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
--- -- Create a new table with only even values
|
2024-04-30 04:30:21 -07:00
|
|
|
--- 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)
|
2024-05-14 16:18:33 -07:00
|
|
|
--- acc.max = math.max(v, acc.max or v)
|
|
|
|
--- return acc
|
2024-04-30 04:30:21 -07:00
|
|
|
--- end) --> { max = 42 }
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
---
|
2023-04-17 11:54:19 -07:00
|
|
|
---@generic A
|
|
|
|
---
|
|
|
|
---@param init A Initial value of the accumulator.
|
2024-01-09 10:36:46 -07:00
|
|
|
---@param f fun(acc:A, ...):A Accumulation function.
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return A
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:fold(init, f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local acc = init
|
|
|
|
|
|
|
|
--- Use a closure to handle var args returned from iterator
|
|
|
|
local function fn(...)
|
|
|
|
if select(1, ...) ~= nil then
|
|
|
|
acc = f(acc, ...)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
while fn(self:next()) do
|
|
|
|
end
|
|
|
|
return acc
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:fold(init, f)
|
2023-04-17 11:54:19 -07:00
|
|
|
local acc = init
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
acc = f(acc, unpack(self._table[i]))
|
|
|
|
end
|
|
|
|
return acc
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Gets the next value from the iterator.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter(string.gmatch('1 2 3', '%d+')):map(tonumber)
|
|
|
|
--- it:next()
|
|
|
|
--- -- 1
|
|
|
|
--- it:next()
|
|
|
|
--- -- 2
|
|
|
|
--- it:next()
|
|
|
|
--- -- 3
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@return any
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:next()
|
2023-04-17 11:54:19 -07:00
|
|
|
-- This function is provided by the source iterator in Iter.new. This definition exists only for
|
|
|
|
-- the docstring
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:next()
|
2023-04-17 11:54:19 -07:00
|
|
|
if self._head ~= self._tail then
|
|
|
|
local v = self._table[self._head]
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
self._head = self._head + inc
|
|
|
|
return unpack(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Reverses a |list-iterator| pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter({ 3, 6, 9, 12 }):rev()
|
|
|
|
--- it:totable()
|
|
|
|
--- -- { 12, 9, 6, 3 }
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:rev()
|
2024-05-17 12:17:25 -07:00
|
|
|
error('rev() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:rev()
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
self._head, self._tail = self._tail - inc, self._head - inc
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Gets the next value in a |list-iterator| without consuming it.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
--- it:peek()
|
|
|
|
--- -- 3
|
|
|
|
--- it:peek()
|
|
|
|
--- -- 3
|
|
|
|
--- it:next()
|
|
|
|
--- -- 3
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@return any
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:peek()
|
2024-05-17 12:17:25 -07:00
|
|
|
error('peek() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:peek()
|
2023-04-17 11:54:19 -07:00
|
|
|
if self._head ~= self._tail then
|
|
|
|
return self._table[self._head]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- 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:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- 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
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2024-02-15 10:16:04 -07:00
|
|
|
---@param f any
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return any
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:find(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
if type(f) ~= 'function' then
|
|
|
|
local val = f
|
|
|
|
f = function(v)
|
|
|
|
return v == val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = nil
|
|
|
|
|
|
|
|
--- Use a closure to handle var args returned from iterator
|
|
|
|
local function fn(...)
|
|
|
|
if select(1, ...) ~= nil then
|
|
|
|
if f(...) then
|
|
|
|
result = pack(...)
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
while fn(self:next()) do
|
|
|
|
end
|
|
|
|
return unpack(result)
|
|
|
|
end
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
--- Gets the first value satisfying a predicate, from the end of a |list-iterator|.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Advances the iterator. Returns nil and drains the iterator if no value is found.
|
|
|
|
---
|
|
|
|
--- Examples:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter({ 1, 2, 3, 2, 1 }):enumerate()
|
|
|
|
--- it:rfind(1)
|
|
|
|
--- -- 5 1
|
|
|
|
--- it:rfind(1)
|
|
|
|
--- -- 1 1
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@see Iter.find
|
|
|
|
---
|
2024-02-15 10:16:04 -07:00
|
|
|
---@param f any
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return any
|
2023-12-13 06:04:24 -07:00
|
|
|
---@diagnostic disable-next-line: unused-local
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:rfind(f) -- luacheck: no unused args
|
2024-05-17 12:17:25 -07:00
|
|
|
error('rfind() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:rfind(f)
|
2023-04-17 11:54:19 -07:00
|
|
|
if type(f) ~= 'function' then
|
|
|
|
local val = f
|
|
|
|
f = function(v)
|
|
|
|
return v == val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
for i = self._tail - inc, self._head, -inc do
|
|
|
|
local v = self._table[i]
|
|
|
|
if f(unpack(v)) then
|
|
|
|
self._tail = i
|
|
|
|
return unpack(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self._head = self._tail
|
|
|
|
end
|
|
|
|
|
2023-12-12 13:27:24 -07:00
|
|
|
--- Transforms an iterator to yield only the first n values.
|
|
|
|
---
|
|
|
|
--- Example:
|
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- local it = vim.iter({ 1, 2, 3, 4 }):take(2)
|
|
|
|
--- it:next()
|
|
|
|
--- -- 1
|
|
|
|
--- it:next()
|
|
|
|
--- -- 2
|
|
|
|
--- it:next()
|
|
|
|
--- -- nil
|
|
|
|
--- ```
|
|
|
|
---
|
|
|
|
---@param n integer
|
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:take(n)
|
2023-12-12 13:27:24 -07:00
|
|
|
local next = self.next
|
|
|
|
local i = 0
|
|
|
|
self.next = function()
|
|
|
|
if i < n then
|
|
|
|
i = i + 1
|
|
|
|
return next(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:take(n)
|
|
|
|
local inc = self._head < self._tail and n or -n
|
2024-03-23 14:46:54 -07:00
|
|
|
local cmp = self._head < self._tail and math.min or math.max
|
2024-05-17 12:17:25 -07:00
|
|
|
self._tail = cmp(self._tail, self._head + inc)
|
2023-12-12 13:27:24 -07:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- "Pops" a value from a |list-iterator| (gets the last value and decrements the tail).
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- local it = vim.iter({1, 2, 3, 4})
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 4
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 3
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@return any
|
2024-04-26 08:43:29 -07:00
|
|
|
function Iter:pop()
|
2024-05-17 12:17:25 -07:00
|
|
|
error('pop() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
--- @nodoc
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
if self._head ~= self._tail then
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
self._tail = self._tail - inc
|
|
|
|
return self._table[self._tail]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Gets the last value of a |list-iterator| without consuming it.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- local it = vim.iter({1, 2, 3, 4})
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:rpeek()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 4
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:rpeek()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 4
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 4
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
---@see Iter.last
|
|
|
|
---
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return any
|
2024-04-26 08:43:29 -07:00
|
|
|
function Iter:rpeek()
|
2024-05-17 12:17:25 -07:00
|
|
|
error('rpeek() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2024-02-15 10:16:04 -07:00
|
|
|
---@nodoc
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:rpeek()
|
2023-04-17 11:54:19 -07:00
|
|
|
if self._head ~= self._tail then
|
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
return self._table[self._tail - inc]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Skips `n` values of an iterator pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter({ 3, 6, 9, 12 }):skip(2)
|
|
|
|
--- it:next()
|
|
|
|
--- -- 9
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@param n number Number of values to skip.
|
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:skip(n)
|
2023-04-17 11:54:19 -07:00
|
|
|
for _ = 1, n do
|
|
|
|
local _ = self:next()
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:skip(n)
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and n or -n
|
|
|
|
self._head = self._head + inc
|
|
|
|
if (inc > 0 and self._head > self._tail) or (inc < 0 and self._head < self._tail) then
|
|
|
|
self._head = self._tail
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2024-04-26 08:43:29 -07:00
|
|
|
--- Discards `n` values from the end of a |list-iterator| pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2024-04-26 08:43:29 -07:00
|
|
|
--- local it = vim.iter({ 1, 2, 3, 4, 5 }):rskip(2)
|
2023-04-17 11:54:19 -07:00
|
|
|
--- it:next()
|
|
|
|
--- -- 1
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it:pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 3
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@param n number Number of values to skip.
|
|
|
|
---@return Iter
|
2023-12-13 06:04:24 -07:00
|
|
|
---@diagnostic disable-next-line: unused-local
|
2024-04-26 08:43:29 -07:00
|
|
|
function Iter:rskip(n) -- luacheck: no unused args
|
2024-05-17 12:17:25 -07:00
|
|
|
error('rskip() requires an array-like table')
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:rskip(n)
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and n or -n
|
|
|
|
self._tail = self._tail - inc
|
|
|
|
if (inc > 0 and self._head > self._tail) or (inc < 0 and self._head < self._tail) then
|
|
|
|
self._head = self._tail
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Gets the nth value of an iterator (and advances to it).
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
--- If `n` is negative, offsets from the end of a |list-iterator|.
|
|
|
|
---
|
2023-04-17 11:54:19 -07:00
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- local it = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
--- it:nth(2)
|
|
|
|
--- -- 6
|
|
|
|
--- it:nth(2)
|
|
|
|
--- -- 12
|
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
--- local it2 = vim.iter({ 3, 6, 9, 12 })
|
|
|
|
--- it2:nth(-2)
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 9
|
2024-04-26 08:43:29 -07:00
|
|
|
--- it2:nth(-2)
|
2023-04-17 11:54:19 -07:00
|
|
|
--- -- 3
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
---@param n number Index of the value to return. May be negative if the source is a |list-iterator|.
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return any
|
2024-04-26 08:43:29 -07:00
|
|
|
function Iter:nth(n)
|
2023-04-17 11:54:19 -07:00
|
|
|
if n > 0 then
|
2024-04-26 08:43:29 -07:00
|
|
|
return self:skip(n - 1):next()
|
|
|
|
elseif n < 0 then
|
|
|
|
return self:rskip(math.abs(n) - 1):pop()
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Sets the start and end of a |list-iterator| pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
--- Equivalent to `:skip(first - 1):rskip(len - last + 1)`.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@param first number
|
|
|
|
---@param last number
|
|
|
|
---@return Iter
|
2023-12-13 06:04:24 -07:00
|
|
|
---@diagnostic disable-next-line: unused-local
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:slice(first, last) -- luacheck: no unused args
|
2024-05-17 12:17:25 -07:00
|
|
|
error('slice() requires an array-like table')
|
2023-08-09 13:41:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:slice(first, last)
|
2024-04-26 08:43:29 -07:00
|
|
|
return self:skip(math.max(0, first - 1)):rskip(math.max(0, self._tail - last - 1))
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Returns true if any of the items in the iterator match the given predicate.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-01-18 06:01:57 -07:00
|
|
|
---@param pred fun(...):boolean Predicate function. Takes all values returned from the previous
|
2024-01-09 10:36:46 -07:00
|
|
|
--- stage in the pipeline as arguments and returns true if the
|
|
|
|
--- predicate matches.
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:any(pred)
|
2023-04-17 11:54:19 -07:00
|
|
|
local any = false
|
|
|
|
|
|
|
|
--- Use a closure to handle var args returned from iterator
|
|
|
|
local function fn(...)
|
|
|
|
if select(1, ...) ~= nil then
|
|
|
|
if pred(...) then
|
|
|
|
any = true
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
while fn(self:next()) do
|
|
|
|
end
|
|
|
|
return any
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Returns true if all items in the iterator match the given predicate.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-01-18 06:01:57 -07:00
|
|
|
---@param pred fun(...):boolean Predicate function. Takes all values returned from the previous
|
2024-01-09 10:36:46 -07:00
|
|
|
--- stage in the pipeline as arguments and returns true if the
|
|
|
|
--- predicate matches.
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:all(pred)
|
2023-04-17 11:54:19 -07:00
|
|
|
local all = true
|
|
|
|
|
|
|
|
local function fn(...)
|
|
|
|
if select(1, ...) ~= nil then
|
|
|
|
if not pred(...) then
|
|
|
|
all = false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
while fn(self:next()) do
|
|
|
|
end
|
|
|
|
return all
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Drains the iterator and returns the last item.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter(vim.gsplit('abcdefg', ''))
|
|
|
|
--- it:last()
|
|
|
|
--- -- 'g'
|
|
|
|
---
|
|
|
|
--- local it = vim.iter({ 3, 6, 9, 12, 15 })
|
|
|
|
--- it:last()
|
|
|
|
--- -- 15
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-04-26 08:43:29 -07:00
|
|
|
---@see Iter.rpeek
|
|
|
|
---
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return any
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:last()
|
2023-04-17 11:54:19 -07:00
|
|
|
local last = self:next()
|
|
|
|
local cur = self:next()
|
|
|
|
while cur do
|
|
|
|
last = cur
|
|
|
|
cur = self:next()
|
|
|
|
end
|
|
|
|
return last
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:last()
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
local v = self._table[self._tail - inc]
|
|
|
|
self._head = self._tail
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Yields the item index (count) and value for each item of an iterator pipeline.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2023-11-25 07:35:31 -07:00
|
|
|
--- For list tables, this is more efficient:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- vim.iter(ipairs(t))
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2023-11-25 07:35:31 -07:00
|
|
|
--- instead of:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
--- vim.iter(t):enumerate()
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-13 06:38:28 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
--- local it = vim.iter(vim.gsplit('abc', '')):enumerate()
|
|
|
|
--- it:next()
|
|
|
|
--- -- 1 'a'
|
|
|
|
--- it:next()
|
|
|
|
--- -- 2 'b'
|
|
|
|
--- it:next()
|
|
|
|
--- -- 3 'c'
|
|
|
|
---
|
2023-09-13 06:38:28 -07:00
|
|
|
--- ```
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@return Iter
|
2024-03-06 05:15:25 -07:00
|
|
|
function Iter:enumerate()
|
2023-04-17 11:54:19 -07:00
|
|
|
local i = 0
|
|
|
|
return self:map(function(...)
|
|
|
|
i = i + 1
|
|
|
|
return i, ...
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter:enumerate()
|
2023-04-17 11:54:19 -07:00
|
|
|
local inc = self._head < self._tail and 1 or -1
|
|
|
|
for i = self._head, self._tail - inc, inc do
|
|
|
|
local v = self._table[i]
|
2023-04-19 05:45:56 -07:00
|
|
|
self._table[i] = pack(i, v)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-11-25 07:35:31 -07:00
|
|
|
--- Creates a new Iter object from a table or other |iterable|.
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
|
|
|
---@param src table|function Table or iterator to drain values from
|
|
|
|
---@return Iter
|
2023-04-24 18:57:40 -07:00
|
|
|
---@private
|
2023-04-17 11:54:19 -07:00
|
|
|
function Iter.new(src, ...)
|
|
|
|
local it = {}
|
|
|
|
if type(src) == 'table' then
|
2023-06-10 11:33:23 -07:00
|
|
|
local mt = getmetatable(src)
|
|
|
|
if mt and type(mt.__call) == 'function' then
|
|
|
|
---@private
|
|
|
|
function it.next()
|
|
|
|
return src()
|
|
|
|
end
|
|
|
|
|
|
|
|
setmetatable(it, Iter)
|
|
|
|
return it
|
|
|
|
end
|
|
|
|
|
2023-04-17 11:54:19 -07:00
|
|
|
local t = {}
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
-- O(n): scan the source table to decide if it is an array (only positive integer indices).
|
|
|
|
for k, v in pairs(src) do
|
|
|
|
if type(k) ~= 'number' or k <= 0 or math.floor(k) ~= k then
|
fix(iter): remove special case totable for map-like tables
This was originally meant as a convenience but prevents possible
functionality. For example:
-- Get the keys of the table with even values
local t = { a = 1, b = 2, c = 3, d = 4 }
vim.iter(t):map(function(k, v)
if v % 2 == 0 then return k end
end):totable()
The example above would not work, because the map() function returns
only a single value, and cannot be converted back into a table (there
are many such examples like this).
Instead, to convert an iterator into a map-like table, users can use
fold():
vim.iter(t):fold({}, function(t, k, v)
t[k] = v
return t
end)
2023-04-19 06:05:04 -07:00
|
|
|
return Iter.new(pairs(src))
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
2024-09-01 13:01:24 -07:00
|
|
|
t[#t + 1] = v -- Coerce to list-like table.
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
2024-05-17 12:17:25 -07:00
|
|
|
return ArrayIter.new(t)
|
2023-04-17 11:54:19 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
if type(src) == 'function' then
|
|
|
|
local s, var = ...
|
|
|
|
|
|
|
|
--- Use a closure to handle var args returned from iterator
|
|
|
|
local function fn(...)
|
2023-08-09 13:41:45 -07:00
|
|
|
-- Per the Lua 5.1 reference manual, an iterator is complete when the first returned value is
|
|
|
|
-- nil (even if there are other, non-nil return values). See |for-in|.
|
2023-04-17 11:54:19 -07:00
|
|
|
if select(1, ...) ~= nil then
|
|
|
|
var = select(1, ...)
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-24 18:57:40 -07:00
|
|
|
---@private
|
2023-04-17 11:54:19 -07:00
|
|
|
function it.next()
|
|
|
|
return fn(src(s, var))
|
|
|
|
end
|
|
|
|
|
|
|
|
setmetatable(it, Iter)
|
|
|
|
else
|
|
|
|
error('src must be a table or function')
|
|
|
|
end
|
|
|
|
return it
|
|
|
|
end
|
|
|
|
|
2024-05-17 12:17:25 -07:00
|
|
|
--- Create a new ArrayIter
|
2023-04-17 11:54:19 -07:00
|
|
|
---
|
2024-05-17 12:17:25 -07:00
|
|
|
---@param t table Array-like table. Caller guarantees that this table is a valid array. Can have
|
|
|
|
--- holes (nil values).
|
2023-04-17 11:54:19 -07:00
|
|
|
---@return Iter
|
|
|
|
---@private
|
2024-05-17 12:17:25 -07:00
|
|
|
function ArrayIter.new(t)
|
2023-04-17 11:54:19 -07:00
|
|
|
local it = {}
|
|
|
|
it._table = t
|
|
|
|
it._head = 1
|
|
|
|
it._tail = #t + 1
|
2024-05-17 12:17:25 -07:00
|
|
|
setmetatable(it, ArrayIter)
|
2023-04-17 11:54:19 -07:00
|
|
|
return it
|
|
|
|
end
|
|
|
|
|
2023-04-24 18:57:40 -07:00
|
|
|
return setmetatable(M, {
|
|
|
|
__call = function(_, ...)
|
|
|
|
return Iter.new(...)
|
|
|
|
end,
|
2024-03-02 15:39:25 -07:00
|
|
|
}) --[[@as IterMod]]
|