diff options
| author | 2024-08-07 21:51:15 +0200 | |
|---|---|---|
| committer | 2024-08-07 21:51:15 +0200 | |
| commit | 23d6644dbe76e22d8558657bfede7b5d471d6383 (patch) | |
| tree | 6ef0b96be5a3e840548d125a208bca4d262d8827 | |
| parent | nvim: move mini plugin into a single file (diff) | |
nvim: rework mixed-table macro
I didn't like that you couldn't keep mixing and matching, so I made it
so you can.
In the process, I also implemented even?, odd?, filter, mapcat, flat,
and concat.
Some of them didn't end up needing to exist, and the entire :toast.
namespace does need to be cleaned up.
Then again, it's not like I'm selling this like an alternative stdlib.
I should really try and port all of clojure.core though, it'd be funny.
| -rw-r--r-- | dot_config/nvim/fnl/toast/core.fnl | 36 | ||||
| -rw-r--r-- | dot_config/nvim/fnl/toast/macros.fnl | 24 | ||||
| -rw-r--r-- | dot_config/nvim/fnl/toast/table.fnl | 12 | ||||
| -rw-r--r-- | dot_config/nvim/ftplugin/markdown_quote.lua | 87 | ||||
| -rw-r--r-- | dot_config/nvim/lua/plugins/flash.lua | 10 | ||||
| -rw-r--r-- | dot_config/nvim/lua/plugins/os/init.lua | 2 | ||||
| -rw-r--r-- | dot_config/nvim/lua/plugins/telescope.lua | 2 | ||||
| -rw-r--r-- | dot_config/nvim/lua/toast/core.lua | 42 | ||||
| -rw-r--r-- | dot_config/nvim/lua/toast/table.lua | 16 |
9 files changed, 164 insertions, 67 deletions
diff --git a/dot_config/nvim/fnl/toast/core.fnl b/dot_config/nvim/fnl/toast/core.fnl index 82b0c40..11a294b 100644 --- a/dot_config/nvim/fnl/toast/core.fnl +++ b/dot_config/nvim/fnl/toast/core.fnl @@ -18,6 +18,12 @@ (fn number? [n] "Returns true if the argument is a number." (= :number (type n))) +(fn even? [n] + "Returns true if the argument is an even number." + (and (number? n) (= 0 (% n 2)))) +(fn odd? [n] + "Returns true if the argument is an odd number." + (and (number? n) (not= 0 (% n 2)))) (fn every? [pred xs] "Returns true if (pred x) is logical true for every x in xs, else false." @@ -72,6 +78,15 @@ 0 t 1 (error "assoc expects even number of arguments after table, found odd number") _ (assoc t (unpack xs))))) +; TODO: split into filter and kfilter? +(fn filter [f xs] + "Returns a sequential table consisting only of elements in xs that return a truth value when passed to f. + Note that the call will be (f k v), not (f v)." + (accumulate [out [] + k v (ipairs xs)] + (if (f k v) + (insert 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 [] @@ -80,6 +95,20 @@ (insert out (if (= 0 (select :# mapped)) nil mapped))))) +; TODO: rework +(fn mapcat [f xs] + (accumulate [out [] + _ v (ipairs xs)] + (do (each [_ v (ipairs (f v))] + (table.insert out v)) + out))) + +; sequences cont +(fn flat [xs] + "Flattens a sequential table xs such that there are no sub-tables." + (case (type xs) + :table (mapcat flat xs) + _ [xs])) {;math : dec @@ -88,12 +117,17 @@ : empty? : nil? : number? + : even? + : odd? : every? ; sequences : drop : first : last : group + : flat ; HOF : assoc - : map} + : filter + : map + : mapcat} diff --git a/dot_config/nvim/fnl/toast/macros.fnl b/dot_config/nvim/fnl/toast/macros.fnl index e9d5038..8d7f219 100644 --- a/dot_config/nvim/fnl/toast/macros.fnl +++ b/dot_config/nvim/fnl/toast/macros.fnl @@ -2,8 +2,12 @@ (local {: inc : drop - : assoc} (require :toast.core)) -(local {: insert} (require :toast.table)) + : assoc + : filter + : even? + : odd? + : last} (require :toast.core)) +(local {: insert : unpack : concat} (require :toast.table)) (fn mixed-table [...] "Generate a mixed table. @@ -11,12 +15,16 @@ This macro simply expands to the correct data during compile-time. It is recommended to import this as a single character macro locally." (let [args [...] - pre (accumulate [out [] - _ v (ipairs args) - &until (= v '&)] - (insert out v)) - post (drop (inc (length pre)) args)] - (assoc pre (unpack post)))) + ; groups, separated by &s + grps (accumulate [out [[]] + _ v (ipairs args)] + (if (= v '&) + (insert out []) + (do (table.insert (last out) v) + out))) + kvs (concat (unpack (filter even? grps))) + arr (concat (unpack (filter odd? grps)))] + (assoc arr (unpack kvs)))) (fn recc [reqspec key ...] "A common lua pattern is `require 'something'.call(arg1, arg2)`. diff --git a/dot_config/nvim/fnl/toast/table.fnl b/dot_config/nvim/fnl/toast/table.fnl index fe118e8..17934b2 100644 --- a/dot_config/nvim/fnl/toast/table.fnl +++ b/dot_config/nvim/fnl/toast/table.fnl @@ -11,8 +11,18 @@ (table.insert t ...) t) +(fn concat [...] + "Join an arbitrary amount of tables. + Equivalent to a single 'layer' of flat." + (let [out []] + (each [_ xs (ipairs [...])] + (each [_ v (ipairs xs)] + (insert out v))) + out)) + (local unpack (or table.unpack unpack)) {: from-pairs : insert - : unpack} + : unpack + : concat} diff --git a/dot_config/nvim/ftplugin/markdown_quote.lua b/dot_config/nvim/ftplugin/markdown_quote.lua index c1e6b2a..4bcfc33 100644 --- a/dot_config/nvim/ftplugin/markdown_quote.lua +++ b/dot_config/nvim/ftplugin/markdown_quote.lua @@ -2,25 +2,27 @@ local buf = 0 local function location(opts) local sl - local function _1_() - local t_2_ = opts - if (nil ~= t_2_) then - t_2_ = (t_2_).line1 + local _2_ + do + local t_1_ = opts + if (nil ~= t_1_) then + t_1_ = t_1_.line1 else end - return t_2_ + _2_ = t_1_ end - sl = (_1_() or vim.fn.line("v")) + sl = (_2_ or vim.fn.line("v")) local el - local function _4_() - local t_5_ = opts - if (nil ~= t_5_) then - t_5_ = (t_5_).line2 + local _5_ + do + local t_4_ = opts + if (nil ~= t_4_) then + t_4_ = t_4_.line2 else end - return t_5_ + _5_ = t_4_ end - el = (_4_() or vim.fn.line(".")) + el = (_5_ or vim.fn.line(".")) local swap = (sl > el) local dec local function _7_(_241) @@ -49,17 +51,17 @@ local function maplines(f) local setl = argp(vim.api.nvim_buf_set_lines) local getl = argp(vim.api.nvim_buf_get_lines) local function _14_() - local tbl_17_auto = {} - local i_18_auto = #tbl_17_auto + local tbl_21_auto = {} + local i_22_auto = 0 for _, v in ipairs(getl()) do - local val_19_auto = f(v) - if (nil ~= val_19_auto) then - i_18_auto = (i_18_auto + 1) - do end (tbl_17_auto)[i_18_auto] = val_19_auto + local val_23_auto = f(v) + if (nil ~= val_23_auto) then + i_22_auto = (i_22_auto + 1) + tbl_21_auto[i_22_auto] = val_23_auto else end end - return tbl_17_auto + return tbl_21_auto end return setl(_14_()) end @@ -67,19 +69,19 @@ local function maplines(f) end local _2bqf local function _16_(_241) - local _17_ = _241 - if (_17_ == "") then + if (_241 == "") then return _241 else - local function _18_() - local s = _17_ - return s:match("^[%s>]") + local and_17_ = (nil ~= _241) + if and_17_ then + local s = _241 + and_17_ = s:match("^[%s>]") end - if ((nil ~= _17_) and _18_()) then - local s = _17_ + if and_17_ then + local s = _241 return (">" .. s) - elseif (nil ~= _17_) then - local s = _17_ + elseif (nil ~= _241) then + local s = _241 return ("> " .. s) else return nil @@ -89,27 +91,26 @@ end _2bqf = _16_ local _qf local function _20_(_241) - local _21_ = _241 - local function _22_() - local s = _21_ - return s:match("^> ") + local and_21_ = (nil ~= _241) + if and_21_ then + local s = _241 + and_21_ = s:match("^> ") end - if ((nil ~= _21_) and _22_()) then - local s = _21_ + if and_21_ then + local s = _241 return s:sub(3) else - local function _23_() - local s = _21_ - return s:match("^>") + local and_23_ = (nil ~= _241) + if and_23_ then + local s = _241 + and_23_ = s:match("^>") end - if ((nil ~= _21_) and _23_()) then - local s = _21_ + if and_23_ then + local s = _241 return s:sub(2) - elseif true then - local _ = _21_ - return _241 else - return nil + local _ = _241 + return _241 end end end diff --git a/dot_config/nvim/lua/plugins/flash.lua b/dot_config/nvim/lua/plugins/flash.lua index ad4203e..3a8feea 100644 --- a/dot_config/nvim/lua/plugins/flash.lua +++ b/dot_config/nvim/lua/plugins/flash.lua @@ -1,17 +1,17 @@ -- [nfnl] Compiled from fnl/plugins/flash.fnl by https://github.com/Olical/nfnl, do not edit. local function _1_() - return (require("flash")).jump() + return require("flash").jump() end local function _2_() - return (require("flash")).treesitter() + return require("flash").treesitter() end local function _3_() - return (require("flash")).remote() + return require("flash").remote() end local function _4_() - return (require("flash")).treesitter_search() + return require("flash").treesitter_search() end local function _5_() - return (require("flash")).toggle() + return require("flash").toggle() end return {{"folke/flash.nvim", event = "VeryLazy", keys = {{"s", _1_, desc = "Flash", mode = {"n", "x", "o"}}, {"S", _2_, desc = "Flash Treesitter", mode = {"n", "x", "o"}}, {"r", _3_, desc = "Remote Flash", mode = "o"}, {"R", _4_, desc = "Flash Treesitter Search", mode = {"o", "x"}}, {"<c-s>", _5_, desc = "Toggle Flash Search", mode = "c"}}, opts = {modes = {search = {enabled = false}}}}} diff --git a/dot_config/nvim/lua/plugins/os/init.lua b/dot_config/nvim/lua/plugins/os/init.lua index c9b9cab..3cb9f52 100644 --- a/dot_config/nvim/lua/plugins/os/init.lua +++ b/dot_config/nvim/lua/plugins/os/init.lua @@ -1,2 +1,2 @@ -- [nfnl] Compiled from fnl/plugins/os/init.fnl by https://github.com/Olical/nfnl, do not edit. -return require(("plugins.os." .. (jit.os):lower())) +return require(("plugins.os." .. jit.os:lower())) diff --git a/dot_config/nvim/lua/plugins/telescope.lua b/dot_config/nvim/lua/plugins/telescope.lua index 683bdd7..f406c03 100644 --- a/dot_config/nvim/lua/plugins/telescope.lua +++ b/dot_config/nvim/lua/plugins/telescope.lua @@ -1,7 +1,7 @@ -- [nfnl] Compiled from fnl/plugins/telescope.fnl by https://github.com/Olical/nfnl, do not edit. local function gen(key, fun, desc) local function _1_() - return (require("telescope.builtin"))[fun]() + return require("telescope.builtin")[fun]() end return {("<leader>f" .. key), _1_, desc = desc} end diff --git a/dot_config/nvim/lua/toast/core.lua b/dot_config/nvim/lua/toast/core.lua index 6daae14..2e5ba69 100644 --- a/dot_config/nvim/lua/toast/core.lua +++ b/dot_config/nvim/lua/toast/core.lua @@ -17,6 +17,12 @@ end local function number_3f(n) return ("number" == type(n)) end +local function even_3f(n) + return (number_3f(n) and (0 == (n % 2))) +end +local function odd_3f(n) + return (number_3f(n) and (0 ~= (n % 2))) +end local function every_3f(pred, xs) local pass = true for _, x in ipairs(xs) do @@ -108,19 +114,49 @@ local function assoc(_3ft, ...) return assoc(t, unpack(xs)) end end +local function filter(f, xs) + local out = {} + for k, v in ipairs(xs) do + if f(k, v) then + out = insert(out, v) + else + out = out + end + end + return out +end local function map(f, xs) local out = {} for _, v in ipairs((xs or {})) do local mapped = f(v) - local function _15_() + local function _16_() if (0 == select("#", mapped)) then return nil else return mapped end end - out = insert(out, _15_()) + out = insert(out, _16_()) end return out end -return {dec = dec, inc = inc, ["empty?"] = empty_3f, ["nil?"] = nil_3f, ["number?"] = number_3f, ["every?"] = every_3f, drop = drop, first = first, last = last, group = group, assoc = assoc, map = map} +local function mapcat(f, xs) + local out = {} + for _, v in ipairs(xs) do + for _0, v0 in ipairs(f(v)) do + table.insert(out, v0) + end + out = out + end + return out +end +local function flat(xs) + local _17_ = type(xs) + if (_17_ == "table") then + return mapcat(flat, xs) + else + local _ = _17_ + return {xs} + end +end +return {dec = dec, inc = inc, ["empty?"] = empty_3f, ["nil?"] = nil_3f, ["number?"] = number_3f, ["even?"] = even_3f, ["odd?"] = odd_3f, ["every?"] = every_3f, drop = drop, first = first, last = last, group = group, flat = flat, assoc = assoc, filter = filter, map = map, mapcat = mapcat} diff --git a/dot_config/nvim/lua/toast/table.lua b/dot_config/nvim/lua/toast/table.lua index a898a5e..e9c1baf 100644 --- a/dot_config/nvim/lua/toast/table.lua +++ b/dot_config/nvim/lua/toast/table.lua @@ -2,9 +2,8 @@ local function from_pairs(t, _3fmut) local out = (_3fmut or {}) for _, _1_ in ipairs(t) do - local _each_2_ = _1_ - local k = _each_2_[1] - local v = _each_2_[2] + local k = _1_[1] + local v = _1_[2] out[k] = v end return out @@ -13,5 +12,14 @@ local function insert(t, ...) table.insert(t, ...) return t end +local function concat(...) + local out = {} + for _, xs in ipairs({...}) do + for _0, v in ipairs(xs) do + insert(out, v) + end + end + return out +end local unpack = (table.unpack or unpack) -return {["from-pairs"] = from_pairs, insert = insert, unpack = unpack} +return {["from-pairs"] = from_pairs, insert = insert, unpack = unpack, concat = concat} |
