summaryrefslogtreecommitdiff
path: root/dot_config/nvim/fnl/toast
diff options
context:
space:
mode:
Diffstat (limited to 'dot_config/nvim/fnl/toast')
-rw-r--r--dot_config/nvim/fnl/toast/core.fnl47
-rw-r--r--dot_config/nvim/fnl/toast/macros.fnl29
-rw-r--r--dot_config/nvim/fnl/toast/table.fnl15
3 files changed, 91 insertions, 0 deletions
diff --git a/dot_config/nvim/fnl/toast/core.fnl b/dot_config/nvim/fnl/toast/core.fnl
new file mode 100644
index 0000000..54b0455
--- /dev/null
+++ b/dot_config/nvim/fnl/toast/core.fnl
@@ -0,0 +1,47 @@
+(local {: insert} (require :toast.table))
+
+(fn dec [n]
+ "Decrement n by 1."
+ (- n 1))
+(fn inc [n]
+ "Increment n by 1."
+ (+ n 1))
+(fn drop [n xs]
+ "Returns a table of all but the first n elements in xs."
+ (let [out []]
+ (each [i v (ipairs xs)]
+ (when (> i n) (insert out v)))
+ out))
+(fn first [xs]
+ "The first element in a sequential table."
+ (. xs 1))
+(fn last [xs]
+ "The last element in a sequential table."
+ (. xs (length xs)))
+(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))))
+(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)))))
+
+{: dec
+ : inc
+ : drop
+ : group
+ : first
+ : last}
diff --git a/dot_config/nvim/fnl/toast/macros.fnl b/dot_config/nvim/fnl/toast/macros.fnl
new file mode 100644
index 0000000..bb4375b
--- /dev/null
+++ b/dot_config/nvim/fnl/toast/macros.fnl
@@ -0,0 +1,29 @@
+;; [nfnl-macro]
+
+(local {: inc
+ : drop
+ : group} (require :toast.core))
+(local {: from-pairs
+ : insert} (require :toast.table))
+
+(fn tbl [...]
+ "Generate a mixed table.
+ The format is (tbl 1 2 3 & :a :b) to produce {1; 2; 3; a = 'b'}.
+ This macro simply expands to the correct data during compile-time."
+ (let [args [...]
+ pre (accumulate [out []
+ _ v (ipairs args)
+ &until (= v '&)]
+ (insert out v))
+ post (drop (inc (length pre)) args)]
+ (from-pairs (group 2 post) pre)))
+
+(fn recc [reqspec key ...]
+ "A common lua pattern is `require 'something'.call(arg1, arg2)`.
+ The fennel equivalent is ((require :something).call arg1 arg2)
+ This macro makes it easier to do this elegantly.
+ The equivalent call is (recc :something :call arg1 arg2)"
+ `((. (require ,reqspec) ,key) ,...))
+
+{: tbl
+ : recc}
diff --git a/dot_config/nvim/fnl/toast/table.fnl b/dot_config/nvim/fnl/toast/table.fnl
new file mode 100644
index 0000000..a294b14
--- /dev/null
+++ b/dot_config/nvim/fnl/toast/table.fnl
@@ -0,0 +1,15 @@
+(fn from-pairs [t ?mut]
+ "Convert a list of [k v] pairs to a table.
+ If an initial table ?mut is provided, it will be mutated and returned."
+ (let [out (or ?mut {})]
+ (each [_ [k v] (ipairs t)]
+ (tset out k v))
+ out))
+
+(fn insert [t ...]
+ "Like table.insert, but returns the mutated table."
+ (table.insert t ...)
+ t)
+
+{: from-pairs
+ : insert}