summaryrefslogtreecommitdiff
path: root/dot_config/nvim/fnl/toast/macros.fnl
diff options
context:
space:
mode:
authorChloƩ Vulquin <code@toast.bunkerlabs.net>2024-08-07 21:51:15 +0200
committerChloƩ Vulquin <code@toast.bunkerlabs.net>2024-08-07 21:51:15 +0200
commit23d6644dbe76e22d8558657bfede7b5d471d6383 (patch)
tree6ef0b96be5a3e840548d125a208bca4d262d8827 /dot_config/nvim/fnl/toast/macros.fnl
parentnvim: 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.
Diffstat (limited to 'dot_config/nvim/fnl/toast/macros.fnl')
-rw-r--r--dot_config/nvim/fnl/toast/macros.fnl24
1 files changed, 16 insertions, 8 deletions
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)`.