diff options
| author | William Boman <william@redwill.se> | 2023-01-02 06:47:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-02 06:47:41 +0100 |
| commit | 2953751d1beda5eeec54fd7bb743614c56febc18 (patch) | |
| tree | a98799e3d0cd836f5317ed33e14bc8f47123f716 | |
| parent | fix(ui): do not override existing MasonNormal hl group (#833) (diff) | |
| download | mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar.gz mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar.bz2 mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar.lz mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar.xz mason-2953751d1beda5eeec54fd7bb743614c56febc18.tar.zst mason-2953751d1beda5eeec54fd7bb743614c56febc18.zip | |
feat(functional): add split_every and default_to (#835)
| -rw-r--r-- | lua/mason-core/functional/init.lua | 2 | ||||
| -rw-r--r-- | lua/mason-core/functional/list.lua | 15 | ||||
| -rw-r--r-- | lua/mason-core/functional/logic.lua | 12 | ||||
| -rw-r--r-- | tests/mason-core/functional/list_spec.lua | 15 | ||||
| -rw-r--r-- | tests/mason-core/functional/logic_spec.lua | 6 |
5 files changed, 50 insertions, 0 deletions
diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index 153c40aa..9b52047c 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -62,6 +62,7 @@ _.take = list.take _.drop = list.drop _.drop_last = list.drop_last _.reduce = list.reduce +_.split_every = list.split_every ---@module "mason-core.functional.relation" local relation = lazy_require "mason-core.functional.relation" @@ -80,6 +81,7 @@ _.if_else = logic.if_else _.is_not = logic.is_not _.complement = logic.complement _.cond = logic.cond +_.default_to = logic.default_to ---@module "mason-core.functional.number" local number = lazy_require "mason-core.functional.number" diff --git a/lua/mason-core/functional/list.lua b/lua/mason-core/functional/list.lua index 8c216c4d..275bc61a 100644 --- a/lua/mason-core/functional/list.lua +++ b/lua/mason-core/functional/list.lua @@ -269,4 +269,19 @@ _.reduce = fun.curryN(function(fn, acc, list) 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) + return _ diff --git a/lua/mason-core/functional/logic.lua b/lua/mason-core/functional/logic.lua index 125a94ee..79e02361 100644 --- a/lua/mason-core/functional/logic.lua +++ b/lua/mason-core/functional/logic.lua @@ -69,4 +69,16 @@ _.cond = fun.curryN(function(predicate_transformer_pairs, value) end end, 2) +---@generic T +---@param default_val T +---@param val T? +---@return T +_.default_to = fun.curryN(function(default_val, val) + if val ~= nil then + return val + else + return default_val + end +end, 2) + return _ diff --git a/tests/mason-core/functional/list_spec.lua b/tests/mason-core/functional/list_spec.lua index 6c14e5a9..043f058b 100644 --- a/tests/mason-core/functional/list_spec.lua +++ b/tests/mason-core/functional/list_spec.lua @@ -264,6 +264,21 @@ describe("functional: list", function() assert.spy(add).was_called_with(6, 4) assert.spy(add).was_called_with(10, 5) end) + + it("should split lists", function() + assert.same({ + { 1, 2, 3 }, + { 4, 5, 6 }, + { 7 }, + }, _.split_every(3, { 1, 2, 3, 4, 5, 6, 7 })) + + assert.same({ { 1, 2, 3 } }, _.split_every(5, { 1, 2, 3 })) + assert.same({ { 1 }, { 2 }, { 3 } }, _.split_every(1, { 1, 2, 3 })) + + assert.has_error(function() + _.split_every(0, {}) + end) + end) end) describe("list immutability", function() diff --git a/tests/mason-core/functional/logic_spec.lua b/tests/mason-core/functional/logic_spec.lua index 7c795443..b15fd6ae 100644 --- a/tests/mason-core/functional/logic_spec.lua +++ b/tests/mason-core/functional/logic_spec.lua @@ -54,4 +54,10 @@ describe("functional: logic", function() assert.is_true(_.complement(_.is_nil, "not nil")) assert.is_false(_.complement(_.is_nil, nil)) end) + + it("should default to provided value", function() + local fortytwo = _.default_to(42) + assert.equals(42, fortytwo(nil)) + assert.equals(1337, fortytwo(1337)) + end) end) |
