Skip to content

Commit

Permalink
omg
Browse files Browse the repository at this point in the history
  • Loading branch information
fedemengo committed Apr 1, 2023
1 parent d5f3add commit 1caea80
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
25 changes: 25 additions & 0 deletions fnl/init.fnl
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
(module init)

(global omit
(fn [t keys]
"Returns a copy of <t> without the keys in <keys>"
(local nt {})
(each [k v (pairs t)]
(when (not (in k keys))
(tset nt k v)))
nt))

(global deep-copy
(fn [orig]
(let [copy (if (table? orig)
{}
orig)]
(when (table? orig)
(each [orig-key orig-value (pairs orig)]
(tset copy (deep-copy orig-key) (deep-copy orig-value)))
(setmetatable copy (deep-copy (getmetatable orig))))
copy)))

(global table?
(fn [t]
"Returns true if <t> is a table"
(= (type! t) :table)))

(global lengtht
(fn [t]
"Returns the length of a proper table"
Expand Down
2 changes: 1 addition & 1 deletion fnl/mods/tools/telescope.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@

(var magic
(fn [_opts]
(var opts (or _opts {}))
(var opts (deep-copy (or _opts {})))
(var cwd (utils.buffer_dir))
(tset opts :fcmd_depth 4)
(tset opts :cwd cwd)
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require("impatient").enable_profile()
-- caller = false
--})
--
--log.trace("Hello world", { "foo", "bar" }, 1, 2, 3)
--log.trace("Hello world", { "foo", "bar", key ="value" }, 1, 2, 3)

vim.g["aniseed#env"] = {
module = "init",
Expand Down
14 changes: 14 additions & 0 deletions tests/test.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,17 @@
(assert-eq [4 5 6] nums2 "cons should not modify the element")
(assert-eq [1 2 3 4 5 6] (cons [1 2 3 4 5] 6))

(assert-eq {:a 1 :b 2} (omit {:a 1 :b 2 :c 3} [:c]) "omit should remove the key")
(assert-eq {:z 1} (omit {:a 1 :b 2 :c 3 :z 1} [:a :b :c]) "omit should remove the key")

(assert-eq {:a 1 :b 2 :c 3} (deep-copy {:a 1 :b 2 :c 3}) "deep copy should copy the table")
(var q {:a 1 :b 2 :c 3})
(var t1 (deep-copy q))
(var t2 (deep-copy q))
(assert-eq t1 t2 "deep copied tables should be equal")
(tset t1 :a 2)
(assert-neq t1 t2 "tables should be different")
(assert-neq t1 q "tables should be different")
(assert-eq t2 q "tables should remain equal")
(assert-eq t1 {:a 2 :b 2 :c 3} "deep copy should copy the table")

0 comments on commit 1caea80

Please sign in to comment.