aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/functional/table.lua
blob: 26db162b70f1895223a36200b6eae1fd3681673a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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>
---@return U?
_.prop = fun.curryN(function(index, tbl)
    return tbl[index]
end, 2)

---@param path any[]
---@param tbl table
_.path = fun.curryN(function(path, tbl)
    -- see https://github.com/neovim/neovim/pull/21426
    local value = vim.tbl_get(tbl, unpack(path))
    return value
end, 2)

---@generic T, U
---@param keys T[]
---@param tbl table<T, U>
---@return table<T, U>
_.pick = fun.curryN(function(keys, tbl)
    local ret = {}
    for _, key in ipairs(keys) do
        ret[key] = tbl[key]
    end
    return ret
end, 2)

_.keys = fun.curryN(vim.tbl_keys, 1)
_.size = fun.curryN(vim.tbl_count, 1)

---@generic K, V
---@param tbl table<K, V>
---@return { [1]: K, [2]: V }[]
_.to_pairs = fun.curryN(function(tbl)
    local result = {}
    for k, v in pairs(tbl) do
        result[#result + 1] = { k, v }
    end
    return result
end, 1)

---@generic K, V
---@param pairs { [1]: K, [2]: V }[]
---@return table<K, V>
_.from_pairs = fun.curryN(function(pairs)
    local result = {}
    for _, pair in ipairs(pairs) do
        result[pair[1]] = pair[2]
    end
    return result
end, 1)

---@generic K, V
---@param tbl table<K, V>
---@return table<V, K>
_.invert = fun.curryN(function(tbl)
    local result = {}
    for k, v in pairs(tbl) do
        result[v] = k
    end
    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 K, V
---@param key K
---@param value V
---@param tbl table<K, V>
---@return table<K, V>
_.assoc = fun.curryN(function(key, value, tbl)
    local res = shallow_clone(tbl)
    res[key] = value
    return res
end, 3)

---@generic K, V
---@param key K
---@param tbl table<K, V>
---@return table<K, V>
_.dissoc = fun.curryN(function(key, tbl)
    local res = shallow_clone(tbl)
    res[key] = nil
    return res
end, 2)

return _