From 58c188560c2935d500852ebb03f00f832c61cc72 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Wed, 21 Oct 2020 20:34:39 +0300 Subject: added more macros, and functions to the `core` modules --- macros/core.fnl | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 macros/core.fnl (limited to 'macros/core.fnl') 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} -- cgit v1.2.3