diff options
| -rw-r--r-- | cljlib.fnl | 32 | ||||
| -rw-r--r-- | tests/core.fnl | 20 |
2 files changed, 50 insertions, 2 deletions
@@ -906,7 +906,7 @@ use." res)))))) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Hash map extras ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Hash table extras ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (fn* core.assoc "Associate key `k` with value `v` in `tbl`." @@ -955,6 +955,36 @@ found in the table." (set res not-found))) res)) +(fn* core.keys + "Returns a sequence of the table's keys, in the same order as [`seq`](#seq)." + [tbl] + (let [res []] + (each [k _ (pairs tbl)] + (insert res k)) + res)) + +(fn* core.vals + "Returns a sequence of the table's values, in the same order as [`seq`](#seq)." + [tbl] + (let [res []] + (each [_ v (pairs tbl)] + (insert res v)) + res)) + +(fn* core.find + "Returns the map entry for `key`, or `nil` if key not present." + [tbl key] + (when-some [v (. tbl key)] + [key v])) + +(fn* core.dissoc + "Remove `key` from table `tbl`." + ([tbl] tbl) + ([tbl key] + (doto tbl (tset key nil))) + ([tbl key & keys] + (apply dissoc (dissoc tbl key) keys))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Multimethods ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/tests/core.fnl b/tests/core.fnl index 1921a05..d323a07 100644 --- a/tests/core.fnl +++ b/tests/core.fnl @@ -423,7 +423,25 @@ (assert-not (pcall assoc)) (assert-not (pcall assoc {})) (assert-eq (assoc {} :a 1) {:a 1}) - (assert-eq (assoc {} :a 1 :b 2 :c 3 :d 4) {:a 1 :b 2 :c 3 :d 4}))) + (assert-eq (assoc {} :a 1 :b 2 :c 3 :d 4) {:a 1 :b 2 :c 3 :d 4})) + + (testing "dissoc" + (assert-not (pcall dissoc)) + (assert-eq (dissoc {}) {}) + (assert-eq (dissoc {:a 1 :b 2} :b) {:a 1}) + (assert-eq (dissoc {:a 1 :b 2 :c 3} :a :c) {:b 2})) + + (testing "find, keys and vals" + (assert-not (pcall keys)) + (assert-not (pcall keys {} {} {})) + (assert-not (pcall vals)) + (assert-not (pcall vals {} {} {})) + (assert-not (pcall find)) + (assert-not (pcall find {} {} {})) + (assert-eq (keys {:a 1 :b 2 :c 3}) (hash-set :a :b :c)) + (assert-eq (vals {:a 1 :b 2 :c 3}) (hash-set 1 2 3)) + (assert-eq (find {:a 1 :b 2 :c 3} :c) [:c 3]) + (assert-eq (find {:a 1 :b 2 :c 3} :d) nil))) (deftest function-manipulation (testing "constantly" |