aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/core/functional/logic.lua
blob: 262f04a8195503982650e131adccdf7a3b954fc9 (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
local fun = require "nvim-lsp-installer.core.functional.function"

local _ = {}

---@generic T
---@param predicates (fun(item: T): boolean)[]
---@return fun(item: T): boolean
_.all_pass = fun.curryN(function(predicates, item)
    for i = 1, #predicates do
        if not predicates[i](item) then
            return false
        end
    end
    return true
end, 2)

---@generic T
---@param predicate fun(item: T): boolean
---@param a fun(item: T): any
---@param b fun(item: T): any
---@param value T
_.if_else = fun.curryN(function(predicate, a, b, value)
    if predicate(value) then
        return a(value)
    else
        return b(value)
    end
end, 4)

---@param value boolean
_.is_not = function(value)
    return not value
end

_.cond = fun.curryN(function(predicate_transformer_pairs, value)
    for _, pair in ipairs(predicate_transformer_pairs) do
        local predicate, transformer = pair[1], pair[2]
        if predicate(value) then
            return transformer(value)
        end
    end
end, 2)

return _