From f490412f54b2c9479159bd6481cb85c42fd281ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chlo=C3=A9=20Vulquin?= Date: Wed, 24 Jul 2024 19:34:51 +0200 Subject: nvim: add empty? and update group Until now you couldn't do `(tbl 1 2)`. Now you can. --- dot_config/nvim/fnl/toast/core.fnl | 29 ++++++++++++++--------- dot_config/nvim/lua/toast/core.lua | 48 +++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 32 deletions(-) (limited to 'dot_config') diff --git a/dot_config/nvim/fnl/toast/core.fnl b/dot_config/nvim/fnl/toast/core.fnl index 54b0455..3c77e4a 100644 --- a/dot_config/nvim/fnl/toast/core.fnl +++ b/dot_config/nvim/fnl/toast/core.fnl @@ -12,6 +12,9 @@ (each [i v (ipairs xs)] (when (> i n) (insert out v))) out)) +(fn empty? [xs] + "Returns true if the lengthable contains no elements." + (= 0 (length xs))) (fn first [xs] "The first element in a sequential table." (. xs 1)) @@ -21,27 +24,31 @@ (fn group [n xs] "Group elements in xs in groups of n. Extra elements at the end will sit in the final group. - For example, (group 2 [1 2 3 4 5]) results in [[1 2] [3 4] [5]]." - (let [ll #(length (last $)) - donext #(= (ll $) n)] - (accumulate [out [[]] - _ v (ipairs xs)] - (do (when (donext out) - (insert out [])) - (insert (last out) v) - out)))) + For example, (group 2 [1 2 3 4 5]) results in [[1 2] [3 4] [5]]. + One exception is if xs is empty, in which case an empty collection is returned." + (if (empty? xs) + [] + (let [ll #(length (last $)) + donext #(= (ll $) n)] + (accumulate [out [[]] + _ v (ipairs xs)] + (do (when (donext out) + (insert out [])) + (insert (last out) v + out)))))) (fn map [f xs] "Returns a sequential table consisting of the result of apply f to every item in xs." (accumulate [out [] _ v (ipairs xs)] (let [mapped (f v)] (insert out (if (= 0 (select :# mapped)) - nil - mapped))))) + nil + mapped))))) {: dec : inc : drop + : empty? : group : first : last} diff --git a/dot_config/nvim/lua/toast/core.lua b/dot_config/nvim/lua/toast/core.lua index c666faa..3313de7 100644 --- a/dot_config/nvim/lua/toast/core.lua +++ b/dot_config/nvim/lua/toast/core.lua @@ -17,6 +17,9 @@ local function drop(n, xs) end return out end +local function empty_3f(xs) + return (0 == #xs) +end local function first(xs) return xs[1] end @@ -24,40 +27,43 @@ local function last(xs) return xs[#xs] end local function group(n, xs) - local ll - local function _3_(_241) - return #last(_241) - end - ll = _3_ - local donext - local function _4_(_241) - return (ll(_241) == n) - end - donext = _4_ - local out = {{}} - for _, v in ipairs(xs) do - if donext(out) then - insert(out, {}) - else + if empty_3f(xs) then + return {} + else + local ll + local function _3_(_241) + return #last(_241) end - insert(last(out), v) - out = out + ll = _3_ + local donext + local function _4_(_241) + return (ll(_241) == n) + end + donext = _4_ + local out = {{}} + for _, v in ipairs(xs) do + if donext(out) then + insert(out, {}) + else + end + out = insert(last(out), v, out) + end + return out end - return out end local function map(f, xs) local out = {} for _, v in ipairs(xs) do local mapped = f(v) - local function _6_() + local function _7_() if (0 == select("#", mapped)) then return nil else return mapped end end - out = insert(out, _6_()) + out = insert(out, _7_()) end return out end -return {dec = dec, inc = inc, drop = drop, group = group, first = first, last = last} +return {dec = dec, inc = inc, drop = drop, ["empty?"] = empty_3f, group = group, first = first, last = last} -- cgit v1.2.3