mirror of
https://github.com/neovim/neovim
synced 2025-07-19 10:41:48 +00:00
feat(lua): implement Iter:join() (#26416)
This commit is contained in:
@ -3410,6 +3410,19 @@ Iter:fold({init}, {f}) *Iter:fold()*
|
|||||||
Return: ~
|
Return: ~
|
||||||
any
|
any
|
||||||
|
|
||||||
|
Iter:join({delim}) *Iter:join()*
|
||||||
|
Collect the iterator into a delimited string.
|
||||||
|
|
||||||
|
Each element in the iterator is joined into a string separated by {delim}.
|
||||||
|
|
||||||
|
Consumes the iterator.
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
• {delim} (string) Delimiter
|
||||||
|
|
||||||
|
Return: ~
|
||||||
|
(string)
|
||||||
|
|
||||||
Iter:last() *Iter:last()*
|
Iter:last() *Iter:last()*
|
||||||
Drains the iterator and returns the last item.
|
Drains the iterator and returns the last item.
|
||||||
|
|
||||||
|
@ -356,6 +356,18 @@ function ListIter.totable(self)
|
|||||||
return self._table
|
return self._table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- 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
|
||||||
|
function Iter.join(self, delim)
|
||||||
|
return table.concat(self:totable(), delim)
|
||||||
|
end
|
||||||
|
|
||||||
--- Folds ("reduces") an iterator into a single value.
|
--- Folds ("reduces") an iterator into a single value.
|
||||||
---
|
---
|
||||||
--- Examples:
|
--- Examples:
|
||||||
|
@ -91,6 +91,11 @@ describe('vim.iter', function()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('join()', function()
|
||||||
|
eq('1, 2, 3', vim.iter({1, 2, 3}):join(', '))
|
||||||
|
eq('a|b|c|d', vim.iter(vim.gsplit('a|b|c|d', '|')):join('|'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('next()', function()
|
it('next()', function()
|
||||||
local it = vim.iter({1, 2, 3}):map(function(v) return 2 * v end)
|
local it = vim.iter({1, 2, 3}):map(function(v) return 2 * v end)
|
||||||
eq(2, it:next())
|
eq(2, it:next())
|
||||||
|
Reference in New Issue
Block a user