diff options
| author | Andrey Orst <andreyorst@gmail.com> | 2020-11-13 22:54:12 +0300 |
|---|---|---|
| committer | Andrey Orst <andreyorst@gmail.com> | 2020-11-13 22:54:12 +0300 |
| commit | 1b309ac016d806d2f9b44540ed5020f5c60c4256 (patch) | |
| tree | fca18ab2e0153a1462da473cd2fe5c9cc0de6a69 /tests/test.fnl | |
| parent | 8493fa29b1848bf93e899e8930c6e8c1c723eece (diff) | |
fix(core): refactoring
- Rename `fn*` to `defn`, `fn&` to `fn+`.
- Do not use `fn+` in the core at all, provide it for convenience.
- Fix bug in `filter` due to incorrect `cons` implementation.
- Update `seq` and `eq` functions in macros
Diffstat (limited to 'tests/test.fnl')
| -rw-r--r-- | tests/test.fnl | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/tests/test.fnl b/tests/test.fnl index d98e1fa..5dc40c1 100644 --- a/tests/test.fnl +++ b/tests/test.fnl @@ -1,10 +1,17 @@ (local test {}) (fn eq-fn [] - `(fn eq# [a# b#] - (if (and (= (type a#) :table) (= (type b#) :table)) - (let [oldmeta# (getmetatable b#)] - (setmetatable b# {:__index (fn [tbl# key#] + "Returns recursive equality function. + +This function is able to compare tables of any depth, even if one of +the tables uses tables as keys." + `(fn eq# [left# right#] + (if (and (= (type left#) :table) (= (type right#) :table)) + (let [oldmeta# (getmetatable right#)] + ;; In case if we'll get something like + ;; (eq {[1 2 3] {:a [1 2 3]}} {[1 2 3] {:a [1 2 3]}}) + ;; we have to do even deeper search + (setmetatable right# {:__index (fn [tbl# key#] (var res# nil) (each [k# v# (pairs tbl#)] (when (eq# k# key#) @@ -12,17 +19,17 @@ (lua :break))) res#)}) (var [res# count-a# count-b#] [true 0 0]) - (each [k# v# (pairs a#)] - (set res# (eq# v# (. b# k#))) + (each [k# v# (pairs left#)] + (set res# (eq# v# (. right# k#))) (set count-a# (+ count-a# 1)) (when (not res#) (lua :break))) (when res# - (each [_# _# (pairs b#)] + (each [_# _# (pairs right#)] (set count-b# (+ count-b# 1))) (set res# (= count-a# count-b#))) - (setmetatable b# oldmeta#) + (setmetatable right# oldmeta#) res#) - (= a# b#)))) + (= left# right#)))) (fn test.assert-eq [expr1 expr2 msg] |