diff options
| author | Andrey Orst <andreyorst@gmail.com> | 2020-10-31 16:35:07 +0300 |
|---|---|---|
| committer | Andrey Orst <andreyorst@gmail.com> | 2020-10-31 16:35:07 +0300 |
| commit | 72eb3263a54c09cdb1a079b2ad0756fcc3800e8a (patch) | |
| tree | fd51884577db724e0f45a45ea665139cafef6a16 /macros/core.fnl | |
| parent | 596089aece8383bd374621babff1a14f99413dba (diff) | |
fix: no need to prefix private fns with -
Diffstat (limited to 'macros/core.fnl')
| -rw-r--r-- | macros/core.fnl | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/macros/core.fnl b/macros/core.fnl index d107ad6..bbff020 100644 --- a/macros/core.fnl +++ b/macros/core.fnl @@ -3,7 +3,7 @@ (local unpack (or table.unpack _G.unpack)) (local insert table.insert) -(fn -check-bindings [bindings] +(fn check-bindings [bindings] (and (assert-compile (sequence? bindings) "expected binding table" []) (assert-compile (= (length bindings) 2) "expected exactly two forms in binding vector." bindings))) @@ -11,7 +11,7 @@ ([bindings then] (if-let bindings then nil)) ([bindings then else] - (-check-bindings bindings) + (check-bindings bindings) (let [[form test] bindings] `(let [tmp# ,test] (if tmp# @@ -21,7 +21,7 @@ (fn* core.when-let [bindings & body] - (-check-bindings bindings) + (check-bindings bindings) (let [[form test] bindings] `(let [tmp# ,test] (if tmp# @@ -32,7 +32,7 @@ ([bindings then] (if-some bindings then nil)) ([bindings then else] - (-check-bindings bindings) + (check-bindings bindings) (let [[form test] bindings] `(let [tmp# ,test] (if (= tmp# nil) @@ -42,7 +42,7 @@ (fn* core.when-some [bindings & body] - (-check-bindings bindings) + (check-bindings bindings) (let [[form test] bindings] `(let [tmp# ,test] (if (= tmp# nil) @@ -51,12 +51,12 @@ ,(unpack body)))))) -(fn -table-type [tbl] +(fn table-type [tbl] (if (sequence? tbl) :seq (table? tbl) :table :else)) -(fn -table-type-fn [] +(fn table-type-fn [] `(fn [tbl#] (let [t# (type tbl#)] (if (= t# :table) @@ -66,7 +66,7 @@ :table)) :else)))) -(fn -seq-fn [] +(fn seq-fn [] `(fn [tbl#] (var assoc# false) (let [res# [] @@ -79,8 +79,8 @@ (if assoc# res# tbl#)))) (fn& core.into [to from] - (let [to-type (-table-type to) - from-type (-table-type from)] + (let [to-type (table-type to) + from-type (table-type from)] (if (and (= to-type :seq) (= from-type :seq)) `(let [to# ,to insert# table.insert] @@ -89,7 +89,7 @@ to#) (= to-type :seq) `(let [to# ,to - seq# ,(-seq-fn) + seq# ,(seq-fn) insert# table.insert] (each [_# v# (ipairs (seq# ,from))] (insert# to# v#)) @@ -108,7 +108,7 @@ (= to-type :table) `(let [to# ,to from# ,from] - (match (,(-table-type-fn) from#) + (match (,(table-type-fn) from#) :seq (each [_# [k# v#] (ipairs from#)] (tset to# k# v#)) :table (each [k# v# (pairs from#)] @@ -118,8 +118,8 @@ `(let [to# ,to from# ,from insert# table.insert - table-type# ,(-table-type-fn) - seq# ,(-seq-fn)] + table-type# ,(table-type-fn) + seq# ,(seq-fn)] (match (table-type# to#) :seq (each [_# v# (ipairs (seq# from#))] (insert# to# v#)) @@ -154,17 +154,26 @@ dispatch-fn (first opts)] `(local ,name (let [multimethods# {}] - (setmetatable {} {:__call - (fn [_# ...] - ,docstring - ((or (. multimethods# (,dispatch-fn ...)) - (. multimethods# :default)) ...)) - :multimethods multimethods#}))))) + (setmetatable + {} + {:__call + (fn [_# ...] + ,docstring + (let [dispatch-value# (,dispatch-fn ...)] + ((or (. multimethods# dispatch-value#) + (. multimethods# :default) + (error (.. "No method in multimethod '" + ,(tostring name) + "' for dispatch value: " + dispatch-value#) 2)) ...))) + :multimethods multimethods#}))))) (fn* core.defmethod [multifn dispatch-val & fn-tail] - `(tset (. (getmetatable ,multifn) :multimethods) - ,dispatch-val - (fn ,(unpack fn-tail)))) + `(let [multifn# ,multifn] + (tset (. (getmetatable multifn#) :multimethods) + ,dispatch-val + (fn ,(unpack fn-tail))) + multifn#)) core |