aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/functional/table.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/mason-core/functional/table.lua')
-rw-r--r--lua/mason-core/functional/table.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/lua/mason-core/functional/table.lua b/lua/mason-core/functional/table.lua
index eb673d64..276a3180 100644
--- a/lua/mason-core/functional/table.lua
+++ b/lua/mason-core/functional/table.lua
@@ -2,6 +2,17 @@ local fun = require "mason-core.functional.function"
local _ = {}
+---@generic T : table
+---@param tbl T
+---@return T
+local function shallow_clone(tbl)
+ local res = {}
+ for k, v in pairs(tbl) do
+ res[k] = v
+ end
+ return res
+end
+
---@generic T, U
---@param index T
---@param tbl table<T, U>
@@ -64,4 +75,36 @@ _.invert = fun.curryN(function(tbl)
return result
end, 1)
+---@generic K, V
+---@param transforms table<K, fun (value: V): V>
+---@param tbl table<K, V>
+---@return table<K, V>
+_.evolve = fun.curryN(function(transforms, tbl)
+ local result = shallow_clone(tbl)
+ for key, value in pairs(tbl) do
+ if transforms[key] then
+ result[key] = transforms[key](value)
+ end
+ end
+ return result
+end, 2)
+
+---@generic T : table
+---@param left T
+---@param right T
+---@return T
+_.merge_left = fun.curryN(function(left, right)
+ return vim.tbl_extend("force", right, left)
+end, 2)
+
+---@generic T : table
+---@param key any
+---@param tbl T
+---@return T
+_.dissoc = fun.curryN(function(key, tbl)
+ local res = shallow_clone(tbl)
+ res[key] = nil
+ return res
+end, 2)
+
return _