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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
local data = require "mason-core.functional.data"
local fun = require "mason-core.functional.function"
local _ = {}
---@generic T
---@param list T[]
---@return T[]
_.reverse = function(list)
local result = {}
for i = #list, 1, -1 do
result[#result + 1] = list[i]
end
return result
end
_.list_not_nil = function(...)
local result = {}
local args = data.table_pack(...)
for i = 1, args.n do
if args[i] ~= nil then
result[#result + 1] = args[i]
end
end
return result
end
---@generic T
---@param predicate fun(item: T): boolean
---@param list T[]
---@return T | nil
_.find_first = fun.curryN(function(predicate, list)
local result
for i = 1, #list do
local entry = list[i]
if predicate(entry) then
return entry
end
end
return result
end, 2)
---@generic T
---@param predicate fun(item: T): boolean
---@param list T[]
---@return boolean
_.any = fun.curryN(function(predicate, list)
for i = 1, #list do
if predicate(list[i]) then
return true
end
end
return false
end, 2)
---@generic T
---@param predicate fun(item: T): boolean
---@param list T[]
---@return boolean
_.all = fun.curryN(function(predicate, list)
for i = 1, #list do
if not predicate(list[i]) then
return false
end
end
return true
end, 2)
---@generic T
---@type fun(filter_fn: (fun(item: T): boolean), items: T[]): T[]
_.filter = fun.curryN(vim.tbl_filter, 2)
---@generic T, U
---@type fun(map_fn: (fun(item: T): U), items: T[]): U[]
_.map = fun.curryN(vim.tbl_map, 2)
---@param tbl table
---@return table
_.flatten = function(tbl)
local result = {}
--- @param _tbl table<any,any>
local function _tbl_flatten(_tbl)
local n = #_tbl
for i = 1, n do
local v = _tbl[i]
if type(v) == "table" then
_tbl_flatten(v)
elseif v then
table.insert(result, v)
end
end
end
_tbl_flatten(tbl)
return result
end
---@generic T
---@param map_fn fun(item: T): Optional
---@param list T[]
---@return any[]
_.filter_map = fun.curryN(function(map_fn, list)
local ret = {}
for i = 1, #list do
map_fn(list[i]):if_present(function(value)
ret[#ret + 1] = value
end)
end
return ret
end, 2)
---@generic T
---@param fn fun(item: T, index: integer)
---@param list T[]
_.each = fun.curryN(function(fn, list)
for k, v in pairs(list) do
fn(v, k)
end
end, 2)
---@generic T
---@type fun(list: T[]): T[]
_.list_copy = _.map(fun.identity)
_.concat = fun.curryN(function(a, b)
if type(a) == "table" then
assert(type(b) == "table", "concat: expected table")
return vim.list_extend(_.list_copy(a), b)
elseif type(a) == "string" then
assert(type(b) == "string", "concat: expected string")
return a .. b
end
end, 2)
---@generic T
---@param value T
---@param list T[]
---@return T[]
_.append = fun.curryN(function(value, list)
local list_copy = _.list_copy(list)
list_copy[#list_copy + 1] = value
return list_copy
end, 2)
---@generic T
---@param value T
---@param list T[]
---@return T[]
_.prepend = fun.curryN(function(value, list)
local list_copy = _.list_copy(list)
table.insert(list_copy, 1, value)
return list_copy
end, 2)
---@generic T
---@generic U
---@param keys T[]
---@param values U[]
---@return table<T, U>
_.zip_table = fun.curryN(function(keys, values)
local res = {}
for i, key in ipairs(keys) do
res[key] = values[i]
end
return res
end, 2)
---@generic T
---@param offset number
---@param value T[]|string
---@return T|string|nil
_.nth = fun.curryN(function(offset, value)
local index = offset < 0 and (#value + (offset + 1)) or offset
if type(value) == "string" then
return string.sub(value, index, index)
else
return value[index]
end
end, 2)
_.head = _.nth(1)
---@generic T
---@param list T[]
---@return T?
_.last = function(list)
return list[#list]
end
---@param value string|any[]
_.length = function(value)
return #value
end
---@generic T
---@param comp fun(item: T): any
---@param list T[]
---@return T[]
_.sort_by = fun.curryN(function(comp, list)
local copied_list = _.list_copy(list)
table.sort(copied_list, function(a, b)
return comp(a) < comp(b)
end)
return copied_list
end, 2)
---@param sep string
---@param list any[]
_.join = fun.curryN(function(sep, list)
return table.concat(list, sep)
end, 2)
---@generic T
---@param id fun(item: T): any
---@param list T[]
---@return T[]
_.uniq_by = fun.curryN(function(id, list)
local set = {}
local result = {}
for i = 1, #list do
local item = list[i]
local uniq_key = id(item)
if not set[uniq_key] then
set[uniq_key] = true
table.insert(result, item)
end
end
return result
end, 2)
---@generic T
---@param predicate fun(item: T): boolean
---@param list T[]
---@return T[][] # [T[], T[]]
_.partition = fun.curryN(function(predicate, list)
local partitions = { {}, {} }
for _, item in ipairs(list) do
table.insert(partitions[predicate(item) and 1 or 2], item)
end
return partitions
end, 2)
---@generic T
---@param n integer
---@param list T[]
---@return T[]
_.take = fun.curryN(function(n, list)
local result = {}
for i = 1, math.min(n, #list) do
result[#result + 1] = list[i]
end
return result
end, 2)
---@generic T
---@param n integer
---@param list T[]
---@return T[]
_.drop = fun.curryN(function(n, list)
local result = {}
for i = n + 1, #list do
result[#result + 1] = list[i]
end
return result
end, 2)
---@generic T
---@param n integer
---@param list T[]
---@return T[]
_.drop_last = fun.curryN(function(n, list)
local result = {}
for i = 1, #list - n do
result[#result + 1] = list[i]
end
return result
end, 2)
---@generic T, U
---@param fn fun(acc: U, item: T): U
---@param acc U
---@param list T[]
---@return U
_.reduce = fun.curryN(function(fn, acc, list)
for i = 1, #list do
acc = fn(acc, list[i])
end
return acc
end, 3)
---@generic T
---@param n integer
---@param list T[]
---@return T[][]
_.split_every = fun.curryN(function(n, list)
assert(n > 0, "n needs to be greater than 0.")
local res = {}
local idx = 1
while idx <= #list do
table.insert(res, { unpack(list, idx, idx + n - 1) })
idx = idx + n
end
return res
end, 2)
---@generic T, U
---@param index fun(item: T): U
---@param list T[]
---@return table<U, T>
_.index_by = fun.curryN(function(index, list)
local res = {}
for _, item in ipairs(list) do
res[index(item)] = item
end
return res
end, 2)
return _
|