summaryrefslogtreecommitdiff
path: root/fn.fnl
diff options
context:
space:
mode:
authorAndrey Orst <andreyorst@gmail.com>2020-10-21 04:43:32 +0300
committerAndrey Orst <andreyorst@gmail.com>2020-10-21 04:43:32 +0300
commita730948601407e2be84e09df833d4265d895f9d8 (patch)
treea5b12e0bbf6446d34c415af92950496143d93093 /fn.fnl
parentcac18405716e2d762de5e4cac7d255e6c1b28520 (diff)
implement Clojure's defn
Diffstat (limited to 'fn.fnl')
-rw-r--r--fn.fnl42
1 files changed, 0 insertions, 42 deletions
diff --git a/fn.fnl b/fn.fnl
deleted file mode 100644
index e937878..0000000
--- a/fn.fnl
+++ /dev/null
@@ -1,42 +0,0 @@
-(fn string? [x]
- (= (type x) "string"))
-
-(fn has-amp? [args]
- (var res false)
- (each [_ s (ipairs args)]
- (when (= (tostring s) "&")
- (set res true)))
- res)
-
-(fn gen-arity [[args & body]]
- (assert-compile (sequence? args) "fn* expects vector as it's parameter list.
-Try wrapping arguments in square brackets." args)
- (let [arg-length (if (has-amp? args) (sym "_") (length args))
- body (list 'let [args [(sym "...")]] (unpack body))]
- (list arg-length body)))
-
-(fn fn* [name doc? ...]
- (assert-compile (not (string? name)) "fn* expects symbol as function name" name)
- (let [docstring (if (string? doc?) doc? nil)
- args (if (sym? name)
- (if (string? doc?) [...] [doc? ...])
- [name doc? ...])
- [x & xs] args]
- (if (sequence? x)
- ;; Ordinary function
- (let [[args & body] args]
- (if (sym? name)
- `(fn ,name ,args ,docstring ,(unpack body))
- `(fn ,args ,docstring ,(unpack body))))
- ;; Multi-arity function
- (list? x)
- (let [bodies []]
- (each [_ arity (ipairs args)]
- (let [[arity body] (gen-arity arity)]
- (table.insert bodies arity)
- (table.insert bodies body)))
- `(fn ,name [...] ,docstring (match (length [...]) ,(unpack bodies)))))))
-
-{: fn*}
-
-;; (import-macros {: fn*} :fn) (macrodebug (fn* f ([a] a)))