Skip to content

Commit 1018d5e

Browse files
committed
feat(lib.iter): add index support to map
This allows for mapping subsequent arrays into their inside values. e.g. ```lua iter.map( { { { foo = 1} } }, 1, 'foo' ) == { 1 } ```
1 parent 72f81b5 commit 1018d5e

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lua/cpp-tools/lib/iter/init.lua

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local M = {}
22

3+
-- TODO: Use an iter mechanism instead of relying on tables everywhere
4+
35
---Counts the lines in a string
46
---@param str string the string to count lines for
57
---@return number line count # The number of lines
@@ -62,10 +64,17 @@ function M.partition(range, pred, proj_beg)
6264
vim.iter(range):map(proj_beg):filter(fp.nah(pred)):totable() or {}
6365
end
6466

65-
---Maps a range using a function or a name of an internal field
67+
-- TODO: Instead of using an overengineered map thingy
68+
-- have a more robust vim.iter mechanism
69+
70+
---Maps a range using a function, index or a name of an internal field
71+
---Using a function will map the current element using the function,
72+
---Using a field name will do `vim.tbl_get(t, name)`
73+
---Using an integer will do `t[idx]`
74+
---e.g. `fp.map({ { foo = 1 } }, 1, 'foo', function(x) return x * 2 end)` will return { 2 }
6675
---@generic T, U
6776
---@param range [`T`] The range to partition
68-
---@param ... string|fun(T): U Mapping functions or field names
77+
---@param ... integer|string|fun(T): U Mapping functions or field names
6978
---@return [U]|[any]
7079
function M.map(range, ...)
7180
local mappings = { ... }
@@ -81,10 +90,15 @@ function M.map(range, ...)
8190
)
8291

8392
return vim.tbl_get(final, mapping)
93+
elseif mapping_type == 'number' then
94+
return final[mapping]
8495
elseif mapping_type == 'function' then
8596
return mapping(final)
8697
else
87-
assert(false, ('The type of mapping must be a function or a string, not [%s]'):format(mapping_type))
98+
assert(
99+
false,
100+
('The type of mapping must be a function, a string or an integer, not [%s]'):format(mapping_type)
101+
)
88102
end
89103
end)
90104
end)
@@ -250,6 +264,7 @@ function M.__test()
250264

251265
assert.are.same(mapped, { 1, 3, 5, 7 })
252266
end)
267+
253268
it('Maps using a single field name', function()
254269
local arr = {
255270
{ foo = 1, bar = 2 },
@@ -262,6 +277,7 @@ function M.__test()
262277
assert.are.same(foos, { 1, 1, 1 })
263278
assert.are.same(bars, { 2, 2, 2 })
264279
end)
280+
265281
it('Maps nested tables', function()
266282
local arr = {
267283
{ foo = { bar = 1, qoox = { foox = 2, boox = 5 } } },
@@ -273,6 +289,7 @@ function M.__test()
273289
assert.are.same(bars, { 1, 2 })
274290
assert.are.same(fooxes, { 2, 1 })
275291
end)
292+
276293
it('Maps with both functions and field names tables', function()
277294
local arr = {
278295
{ foo = { bar = 1, qoox = { foox = 2, boox = 5 } } },
@@ -284,6 +301,30 @@ function M.__test()
284301

285302
assert.are.same(bars, { 2, 4 })
286303
end)
304+
305+
it('Maps using index', function()
306+
local arr1 = {
307+
{ { foo = 1, bar = 2 } },
308+
{ { foo = 1, bar = 2 } },
309+
}
310+
local arr2 = {
311+
{ foo = { 1, 2 } },
312+
{ foo = { 1, 2 } },
313+
}
314+
local arr3 = {
315+
{ { { { 'foo' } } } },
316+
}
317+
318+
local mapped1 = M.map(arr1, 1, 'foo')
319+
local mapped2 = M.map(arr2, 'foo', 2)
320+
local mapped3 = M.map(arr3, 1, 1, 1, 1, function(s)
321+
return ('%sbar'):format(s)
322+
end)
323+
324+
assert.are.same(mapped1, { 1, 1 })
325+
assert.are.same(mapped2, { 2, 2 })
326+
assert.are.same(mapped3, { 'foobar' })
327+
end)
287328
end)
288329
end
289330

0 commit comments

Comments
 (0)