summaryrefslogtreecommitdiff
path: root/macros/core.fnl
diff options
context:
space:
mode:
Diffstat (limited to 'macros/core.fnl')
-rw-r--r--macros/core.fnl56
1 files changed, 56 insertions, 0 deletions
diff --git a/macros/core.fnl b/macros/core.fnl
new file mode 100644
index 0000000..e88d575
--- /dev/null
+++ b/macros/core.fnl
@@ -0,0 +1,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}