diff options
Diffstat (limited to 'doc/macros.md')
| -rw-r--r-- | doc/macros.md | 89 |
1 files changed, 37 insertions, 52 deletions
diff --git a/doc/macros.md b/doc/macros.md index e9cc517..1841b14 100644 --- a/doc/macros.md +++ b/doc/macros.md @@ -18,7 +18,6 @@ Macros for Cljlib that implement various facilities from Clojure. - [`when-let`](#when-let) - [`if-some`](#if-some) - [`when-some`](#when-some) -- [`deep-tostring`](#deep-tostring) ## `fn*` Function signature: @@ -52,7 +51,7 @@ other one or more arguments: ([] nil) ([x & xs] (print x) - (f (unpack xs)))) + (f ((or table.unpack _G.unpack) xs)))) ``` Note, that this function is recursive, and calls itself with less and @@ -126,7 +125,7 @@ namespace tables: (fn* ns.strings.join ([s1 s2] (.. s1 s2)) ([s1 s2 & strings] - (join (join s1 s2) (unpack strings)))) ;; call `join` resolves to ns.strings.join + (join (join s1 s2) ((or table.unpack _G.unpack) strings)))) ;; call `join` resolves to ns.strings.join (fn* ns.tables.join ([t1 t2] @@ -135,14 +134,8 @@ namespace tables: (each [_ v (ipairs t2)] (table.insert res v)) res)) ([t1 t2 & tables] - (join (join t1 t2) (unpack tables)))) ;; call to `join` resolves to ns.tables.join -``` + (join (join t1 t2) ((or table.unpack _G.unpack) tables)))) ;; call to `join` resolves to ns.tables.join -Note that this creates a collision and local `join` overrides `join` -from `ns.strings`, so the latter must be fully qualified -`ns.strings.join` when called outside of the function: - -``` fennel (ns.strings.join "a" "b" "c") ;; => abc (join ["a"] ["b"] ["c"] ["d" "e"]) @@ -151,6 +144,10 @@ from `ns.strings`, so the latter must be fully qualified ;; {} ``` +Note that this creates a collision and local `join` overrides `join` +from `ns.strings`, so the latter must be fully qualified +`ns.strings.join` when called outside of the function. + ## `try` Function signature: @@ -193,29 +190,30 @@ Catch all errors, ignore those and return fallback value: Catch error and do cleanup: ``` fennel ->> (let [tbl []] - (try - (table.insert tbl "a") - (table.insert tbl "b" "c") - (catch _ - (each [k _ (pairs tbl)] - (tset tbl k nil)))) - tbl) -{} +(let [tbl []] + (try + (table.insert tbl "a") + (table.insert tbl "b" "c") + (catch _ + (each [k _ (pairs tbl)] + (tset tbl k nil)))) + tbl) +;; => {} ``` Always run some side effect action: ``` fennel ->> (local res (try 10 (finally (print "side-effect!"))) -side-effect! -nil ->> rese0 ->> (local res (try (error 10) (catch 10 nil) (finally (print "side-effect!"))) -side-effect! -nil ->> res -nil +(local res (try 10 (finally (print "side-effect!")))) +;; => side-effect! +;; => nil +res +;; => 10 +(local res (try (error 10) (catch 10 nil) (finally (print "side-effect!")))) +;; => side-effect! +;; => nil +res +;; => nil ``` @@ -247,7 +245,6 @@ supported, which is `:mutable`, which allows mutating variable with ``` fennel ;; Bad, will override existing documentation for 299792458 (if any) (def {:doc "speed of light in m/s"} c 299792458) -(set c 0) ;; => error, can't mutate `c` (def :mutable address "Lua St.") ;; same as (def {:mutable true} address "Lua St.") (set address "Lisp St.") ;; can mutate `address` @@ -357,19 +354,16 @@ tables to Lua's one: (.. "{" (table.concat res ", ") "}"))) (defmethod to-lua-str :string [x] (.. "\"" x "\"")) (defmethod to-lua-str :default [x] (tostring x)) -``` - -And if we call it on some table, we'll get a valid Lua table: -``` fennel (print (to-lua-str {:a {:b 10}})) -;; prints {["a"] = {["b"] = 10}} +;; => {["a"] = {["b"] = 10}} (print (to-lua-str [:a :b :c [:d {:e :f}]])) -;; prints {[1] = "a", [2] = "b", [3] = "c", [4] = {[1] = "d", [2] = {["e"] = "f"}}} +;; => {[1] = "a", [2] = "b", [3] = "c", [4] = {[1] = "d", [2] = {["e"] = "f"}}} ``` -Which we can then reformat as we want and use in Lua if we want. +And if we call it on some table, we'll get a valid Lua table, which we +can then reformat as we want and use in Lua if we want. ## `into` Function signature: @@ -479,12 +473,12 @@ Attach metadata to a value. When metadata feature is not enabled, returns the value without additional metadata. ``` fennel ->> (local foo (with-meta (fn [...] (let [[x y z] [...]] (+ x y z))) - {:fnl/arglist ["x" "y" "z" "..."] - :fnl/docstring "sum first three values"})) ->> (doc foo) -(foo x y z ...) - sum first three values +(local foo (with-meta (fn [...] (let [[x y z] [...]] (+ x y z))) + {:fnl/arglist ["x" "y" "z" "..."] + :fnl/docstring "sum first three values"})) +;; (doc foo) +;; => (foo x y z ...) +;; => sum first three values ``` ## `meta` @@ -500,7 +494,7 @@ feature is not enabled returns `nil`. ### Example ``` fennel ->> (meta (with-meta {} {:meta "data"})) +(meta (with-meta {} {:meta "data"})) ;; => {:meta "data"} ``` @@ -567,15 +561,6 @@ Function signature: If test is non-`nil`, evaluates `body` in implicit `do`. -## `deep-tostring` -Function signature: - -``` -(deep-tostring data key?) -``` - -**Undocumented** - --- |