summaryrefslogtreecommitdiff
path: root/dot_config/nvim/fnl/toast/macros.fnl
blob: 8d7f219609163ad2acc75491d78c9208a440a14f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; [nfnl-macro]

(local {: inc
        : drop
        : assoc
        : filter
        : even?
        : odd?
        : last} (require :toast.core))
(local {: insert : unpack : concat} (require :toast.table))

(fn mixed-table [...]
  "Generate a mixed table.
   The format is (mixed-table 1 2 3 & :a :b) to produce {1; 2; 3; a = 'b'}.
   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 [...]
       ; 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)`.
   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) ,...))

{: mixed-table
 : recc}