diff options
author | Gregory Anders <greg@gpanders.com> | 2023-04-19 07:05:04 -0600 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2023-04-19 07:52:04 -0600 |
commit | 94894068794dbb99804cda689b6c37e70376c8ca (patch) | |
tree | 1dbfb55be2c53077e9318b6171b2113bf3628340 /test/functional/lua/vim_spec.lua | |
parent | 6b96122453fda22dc44a581af1d536988c1adf41 (diff) | |
download | rneovim-94894068794dbb99804cda689b6c37e70376c8ca.tar.gz rneovim-94894068794dbb99804cda689b6c37e70376c8ca.tar.bz2 rneovim-94894068794dbb99804cda689b6c37e70376c8ca.zip |
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)
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r-- | test/functional/lua/vim_spec.lua | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 07b0f0340a..e37d477376 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -3374,13 +3374,18 @@ describe('lua stdlib', function() end) it('handles map-like tables', function() - local t = { a = 1, b = 2, c = 3 } - local it = vim.iter(t):map(function(k, v) + local it = vim.iter({ a = 1, b = 2, c = 3 }):map(function(k, v) if v % 2 ~= 0 then return k:upper(), v * 2 end end) - eq({ A = 2, C = 6 }, it:totable()) + + local t = it:fold({}, function(t, k, v) + t[k] = v + return t + end) + eq({ A = 2, C = 6 }, t) + end) it('handles table values mid-pipeline', function() local map = { |