From fbc72a0c2fe16a93b18ce8facdc9b66e7183f75d Mon Sep 17 00:00:00 2001 From: William Boman Date: Sat, 10 Dec 2022 19:55:41 +0100 Subject: feat(functional): add some more functions (#755) --- lua/mason-core/functional/function.lua | 18 ++++++++++++++ lua/mason-core/functional/init.lua | 8 +++++++ lua/mason-core/functional/list.lua | 25 ++++++++++++++++++++ lua/mason-core/functional/string.lua | 10 ++++++++ lua/mason-core/functional/table.lua | 43 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) (limited to 'lua') diff --git a/lua/mason-core/functional/function.lua b/lua/mason-core/functional/function.lua index 6f1806db..863df28e 100644 --- a/lua/mason-core/functional/function.lua +++ b/lua/mason-core/functional/function.lua @@ -119,4 +119,22 @@ _.converge = _.curryN(function(fn, fns, val) return fn(unpack(vim.tbl_map(_.apply_to(val), fns))) end, 3) +---@param spec table +---@param value any +---@return table +_.apply_spec = _.curryN(function(spec, value) + spec = vim.deepcopy(spec) + local function transform(item) + if type(item) == "table" then + for k, v in pairs(item) do + item[k] = transform(v) + end + return item + else + return item(value) + end + end + return transform(spec) +end, 2) + return _ diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index f03eb3d5..dbb27ddd 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -31,6 +31,7 @@ _.tap = fun.tap _.apply_to = fun.apply_to _.apply = fun.apply _.converge = fun.converge +_.apply_spec = fun.apply_spec ---@module "mason-core.functional.list" local list = lazy_require "mason-core.functional.list" @@ -39,6 +40,7 @@ _.list_not_nil = list.list_not_nil _.list_copy = list.list_copy _.find_first = list.find_first _.any = list.any +_.all = list.all _.filter = list.filter _.map = list.map _.filter_map = list.filter_map @@ -58,6 +60,7 @@ _.join = list.join _.partition = list.partition _.take = list.take _.drop = list.drop +_.drop_last = list.drop_last ---@module "mason-core.functional.relation" local relation = lazy_require "mason-core.functional.relation" @@ -97,6 +100,8 @@ _.gsub = string.gsub _.trim = string.trim _.dedent = string.dedent _.starts_with = string.starts_with +_.to_upper = string.to_upper +_.to_lower = string.to_lower ---@module "mason-core.functional.table" local tbl = lazy_require "mason-core.functional.table" @@ -108,6 +113,9 @@ _.size = tbl.size _.to_pairs = tbl.to_pairs _.from_pairs = tbl.from_pairs _.invert = tbl.invert +_.evolve = tbl.evolve +_.merge_left = tbl.merge_left +_.dissoc = tbl.dissoc ---@module "mason-core.functional.type" local typ = lazy_require "mason-core.functional.type" diff --git a/lua/mason-core/functional/list.lua b/lua/mason-core/functional/list.lua index b40e0046..7c66ccac 100644 --- a/lua/mason-core/functional/list.lua +++ b/lua/mason-core/functional/list.lua @@ -53,6 +53,19 @@ _.any = fun.curryN(function(predicate, list) 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) @@ -232,4 +245,16 @@ _.drop = fun.curryN(function(n, list) 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) + return _ diff --git a/lua/mason-core/functional/string.lua b/lua/mason-core/functional/string.lua index 9b7da979..8c66dbc0 100644 --- a/lua/mason-core/functional/string.lua +++ b/lua/mason-core/functional/string.lua @@ -75,4 +75,14 @@ _.starts_with = fun.curryN(function(prefix, str) return vim.startswith(str, prefix) end, 2) +---@param str string +_.to_upper = function(str) + return str:upper() +end + +---@param str string +_.to_lower = function(str) + return str:lower() +end + return _ 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 @@ -64,4 +75,36 @@ _.invert = fun.curryN(function(tbl) return result end, 1) +---@generic K, V +---@param transforms table +---@param tbl table +---@return table +_.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 _ -- cgit v1.2.3-70-g09d2