From 644fedfa61ad2372cc3b99e0ba18962433340a42 Mon Sep 17 00:00:00 2001 From: Andrey Orst Date: Sat, 21 Nov 2020 19:53:29 +0300 Subject: feature(core): more hash table functions --- cljlib.fnl | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'cljlib.fnl') diff --git a/cljlib.fnl b/cljlib.fnl index f400340..f649b1a 100644 --- a/cljlib.fnl +++ b/cljlib.fnl @@ -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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3