From 35a47b437a9487fdde1f419340d7880be9008720 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Tue, 26 Jan 2021 09:30:07 +0300 Subject: fix: allow defining methods with fn* --- macros.fnl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'macros.fnl') diff --git a/macros.fnl b/macros.fnl index 3f5eb5f..c009144 100644 --- a/macros.fnl +++ b/macros.fnl @@ -3,6 +3,9 @@ (fn first [tbl] (. tbl 1)) +(fn last [tbl] + (. tbl (length tbl))) + (fn rest [tbl] [((or table.unpack _G.unpack) tbl 2)]) @@ -19,9 +22,10 @@ ;; (multisym->sym a.b.c) ;; => (c true) ;; (multisym->sym a) ;; => (a false) ;; ``` - (if (multi-sym? s) - (values (sym (string.gsub (tostring s) ".*%." "")) true) - (values s false))) + (let [parts (multi-sym? s)] + (if parts + (values (sym (last parts)) true) + (values s false)))) (fn contains? [tbl x] ;; Checks if `x` is stored in `tbl` in linear time. @@ -388,6 +392,12 @@ returns the value without additional metadata. (. amp-bodies 1)) fname)))) +(fn demethodize [s] + (-> s + tostring + (string.gsub ":" ".") + sym)) + (fn fn* [name doc? ...] "Create (anonymous) function of fixed arity. Accepts optional `name` and `docstring?` as first two arguments, @@ -517,12 +527,12 @@ from `ns.strings`, so the latter must be fully qualified (let [docstring (if (string? doc?) doc? nil) (name-wo-namespace namespaced?) (multisym->sym name) fname (if (sym? name-wo-namespace) (tostring name-wo-namespace)) + name (demethodize name) args (if (sym? name-wo-namespace) (if (string? doc?) [...] [doc? ...]) [name-wo-namespace doc? ...]) arglist-doc (gen-arglist-doc args) [x] args - body (if (sequence? x) (single-arity-body args fname) (list? x) (multi-arity-body args fname) (assert-compile false "fn*: expected parameters table. -- cgit v1.2.3