diff options
| -rw-r--r-- | core.fnl | 19 | ||||
| -rw-r--r-- | test/core.fnl | 9 |
2 files changed, 24 insertions, 4 deletions
@@ -428,17 +428,28 @@ oppisite truth value." (fn* core.assoc "Associate key `k' with value `v' in `tbl'." - ([tbl k v] (doto tbl (tset k v))) + ([tbl k v] + (setmetatable + (doto tbl (tset k v)) + {:cljlib/table-type :table})) ([tbl k v & kvs] - (assert (zero? (% (length kvs) 2)) "expected even amount key-value args") + (assert (= (% (length kvs) 2) 0) + (.. "no value supplied for key " (. kvs (length kvs)))) (tset tbl k v) - (var [i k v] [1 nil nil]) + (var [k v] [nil nil]) (var (i k) (next kvs)) (while i (set (i v) (next kvs i)) (tset tbl k v) (set (i k) (next kvs i))) - tbl)) + (setmetatable tbl {:cljlib/table-type :table}))) + +(fn& core.hash-map + "Create associative table from keys and values" + [...] + (if (> (select :# ...) 0) + (assoc {} ...) + (setmetatable {} {:cljlib/table-type :table}))) (fn* core.get "Get value from the table by accessing it with a `key'. diff --git a/test/core.fnl b/test/core.fnl index 0609788..54b2d00 100644 --- a/test/core.fnl +++ b/test/core.fnl @@ -4,6 +4,7 @@ (local {: vec + : hash-map : apply : seq : first @@ -599,3 +600,11 @@ (assert-eq (vec 1) [1]) (assert-eq (vec 1 2 3) [1 2 3]) (assert-eq (getmetatable (vec 1 2 3)) {:cljlib/table-type :seq}))) + +(deftest hash-map + (testing hash-map + (assert* (not (pcall hash-map :a))) + (assert-eq (hash-map) {}) + (assert-eq (hash-map :a 1) {:a 1}) + (assert-eq (hash-map :a 1 :b 2 :c 3) {:a 1 :b 2 :c 3}) + (assert-eq (getmetatable (hash-map)) {:cljlib/table-type :table}))) |