summaryrefslogtreecommitdiff
path: root/macros/core.fnl
blob: e88d5751d64420c9a1ccf6da34b85843846b8fda (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(import-macros {: fn*} :macros.fn)
(local _unpack (or table.unpack unpack))

(fn check-bindings [bindings]
  (assert-compile (sequence? bindings) "expected binding table

* Try placing a table here in square brackets containing identifiers to bind." bindings)
  (assert-compile (= (length bindings) 2) "expected exactly two forms in binding vector." bindings))

(fn* if-let
  ([bindings then]
   (if-let bindings then 'nil))
  ([bindings then else]
   (check-bindings bindings)
   (let [[form test] bindings]
     `(let [tmp# ,test]
        (if tmp#
            (let [,form tmp#]
              ,then)
            ,else)))))

(fn* when-let
  [bindings & body]
  (check-bindings bindings)
  (let [[form test] bindings]
    `(let [tmp# ,test]
       (if tmp#
           (let [,form tmp#]
             ,(_unpack body))))))

(fn* if-some
  ([bindings then]
   (if-some bindings then 'nil))
  ([bindings then else]
   (check-bindings bindings)
   (let [[form test] bindings]
     `(let [tmp# ,test]
        (if (= tmp# nil)
            ,else
            (let [,form tmp#]
              ,then))))))

(fn* when-some
  [bindings & body]
  (check-bindings bindings)
  (let [[form test] bindings]
    `(let [tmp# ,test]
       (if (= tmp# nil)
           nil
           (let [,form tmp#]
             ,(_unpack body))))))

{: if-let
 : when-let
 : if-some
 : when-some}