diff options
| author | Andrey Orst <andreyorst@gmail.com> | 2020-10-21 20:34:39 +0300 |
|---|---|---|
| committer | Andrey Orst <andreyorst@gmail.com> | 2020-10-21 20:34:39 +0300 |
| commit | 58c188560c2935d500852ebb03f00f832c61cc72 (patch) | |
| tree | 6e783535473649b7d44c7eb80b603e0a06dca826 /macros/core.fnl | |
| parent | 46f472901768d53ad62f9313a977c5ff006a041c (diff) | |
added more macros, and functions to the `core` modules
Diffstat (limited to 'macros/core.fnl')
| -rw-r--r-- | macros/core.fnl | 56 |
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} |