diff options
| author | ruki <[email protected]> | 2021-11-14 22:51:32 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-11-14 22:51:32 +0800 |
| commit | 8ea70c741c233e8ddc1523cc8f2e91eb7baa2de7 (patch) | |
| tree | 63790e889168914c523e85901d3cae3c97aef81a | |
| parent | 3a6d721b691b8235dd6189d6e24a59537640f702 (diff) | |
add table.remove_if
| -rw-r--r-- | tests/modules/table/test.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/table.lua | 60 |
2 files changed, 33 insertions, 31 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua new file mode 100644 index 000000000..fc17fcb62 --- /dev/null +++ b/tests/modules/table/test.lua @@ -0,0 +1,4 @@ +function test_remove_if(t) + t:are_equal(table.remove_if({1, 2, 3, 4, 5, 6}, function (t, i, v) return (v % 2) == 0 end), {1, 3, 5}) + t:are_equal(table.remove_if({a = 1, b = 2, c = 3}, function (t, i, v) return (v % 2) == 0 end), {a = 1, c = 3}) +end diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua index 21fa185a0..4f59e7a65 100644 --- a/xmake/core/base/table.lua +++ b/xmake/core/base/table.lua @@ -276,9 +276,6 @@ end -- usage: table.to_array(ipairs("a", "b")) -> {{1,"a",n=2},{2,"b",n=2}},2 -- usage: table.to_array(io.lines("file")) -> {"line 1","line 2", ... , "line n"},n function table.to_array(iterator, state, var) - - assert(iterator) - local result = {} local count = 0 while true do @@ -383,13 +380,10 @@ end table.unpack = table.unpack or unpack -- get keys of a table -function table.keys(tab) - - assert(tab) - +function table.keys(tbl) local keyset = {} local n = 0 - for k, _ in pairs(tab) do + for k, _ in pairs(tbl) do n = n + 1 keyset[n] = k end @@ -397,8 +391,8 @@ function table.keys(tab) end -- get order keys of a table -function table.orderkeys(tab) - local keys = table.keys(tab) +function table.orderkeys(tbl) + local keys = table.keys(tbl) table.sort(keys) return keys end @@ -419,13 +413,10 @@ function table.orderpairs(t) end -- get values of a table -function table.values(tab) - - assert(tab) - +function table.values(tbl) local valueset = {} local n = 0 - for _, v in pairs(tab) do + for _, v in pairs(tbl) do n = n + 1 valueset[n] = v end @@ -433,24 +424,16 @@ function table.values(tab) end -- map values to a new table -function table.map(tab, mapper) - - assert(tab) - assert(mapper) - - local newtab = {} - for k, v in pairs(tab) do - newtab[k] = mapper(k, v) +function table.map(tbl, mapper) + local newtbl = {} + for k, v in pairs(tbl) do + newtbl[k] = mapper(k, v) end - return newtab + return newtbl end -- map values to a new array function table.imap(arr, mapper) - - assert(arr) - assert(mapper) - local newarr = {} for k, v in ipairs(arr) do table.insert(newarr, mapper(k, v)) @@ -460,9 +443,6 @@ end -- reverse table values function table.reverse(arr) - - assert(arr) - local revarr = {} local l = #arr for i = 1, l do @@ -471,5 +451,23 @@ function table.reverse(arr) return revarr end +-- remove values if predicate is matched +function table.remove_if(tbl, pred) + if table.is_array(tbl) then + for i = #tbl, 1, -1 do + if pred(tbl, i, tbl[i]) then + table.remove(tbl, i) + end + end + else + for k, v in pairs(tbl) do + if pred(tbl, k, v) then + tbl[k] = nil + end + end + end + return tbl +end + -- return module: table return table |
