about summary refs log tree commit diff
path: root/fnl/nvrc/macro/toolkit.fnl
blob: 8a2bc503cae12c0b6b3d98e8e3a3e504065f455a (plain)
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
38
39
(local {: format} string)

(fn ->str [x]
  (tostring x))

(fn head [xs]
  (. xs 1))

(fn fn? [x]
  "Returns whether the parameter(s) is a function.
  A function is defined as any list with 'fn or 'hashfn as
  their first element."
  (and (list? x) (or (= `fn (head x)) (= `hashfn (head x)))))

(lambda gensym-checksum [...]
  "Generates a new symbol from the checksum of the object
  passed as a parameter.
  The parameter first is casted into a string using the
  function `fennel.view`.
  If only one parameter is passed to the function the return
  value is the checksum as a symbol.
  If two parameters are passed, the first one is considered
  the prefix.
  If three parameters are passed, the first one is considered
  the prefix and the last one is considered the suffix.
  This function depends on the md5 library and the fennel library."
  (match [...]
    [prefix object suffix] (let [{: view} (require :fennel)
                                 {:sumhexa md5} (require :md5)]
                              (sym (.. prefix (md5 (view object)) suffix)))
    [prefix object] (gensym-checksum prefix object "")
    [object] (gensym-checksum "" object "")))

(lambda vlua [x]
  "Return a symbol mapped to `v:lua.%s()`, where `%s` is the symbol."
  (assert-compile (sym? x) "expected symbol for x" x)
  (format "v:lua.%s()" (->str x)))

{: fn? : gensym-checksum : vlua}