diff options
| -rw-r--r-- | core.fnl | 14 | ||||
| -rw-r--r-- | core_test.fnl | 21 |
2 files changed, 22 insertions, 13 deletions
@@ -397,7 +397,7 @@ oppisite truth value." (set (i k) (next kvs i))) tbl)) -(fn& core.get +(fn* core.get "Get value from the table by accessing it with a `key'. Accepts additional `not-found' as a marker to return if value wasn't found in the table." @@ -407,14 +407,18 @@ found in the table." res not-found))) -(fn& core.get-in +(fn* core.get-in "Get value from nested set of tables by providing key sequence. Accepts additional `not-found' as a marker to return if value wasn't found in the table." ([tbl keys] (get-in tbl keys nil)) ([tbl keys not-found] - (if-some [res (. tbl (. keys 1))] - (get-in tbl [(unpack tbl 2)] not-found) - not-found))) + (var res tbl) + (var t tbl) + (each [_ k (ipairs keys)] + (if-some [v (. t k)] + (set [res t] [v v]) + (set res not-found))) + res)) core diff --git a/core_test.fnl b/core_test.fnl index d607935..c618e9e 100644 --- a/core_test.fnl +++ b/core_test.fnl @@ -433,13 +433,18 @@ (assert-eq (get {:key1 10 :key2 20} :key1) 10) (assert-eq (get {:key1 10 :key2 20} :key1 false) 10) (assert-eq (get {:key1 10 :key2 20} :key3 false) false) - (assert-eq (get {:key1 10 :key2 20} :key3) nil)) + (assert-eq (get {:key1 10 :key2 20} :key3) nil) + (assert* (not (pcall get))) + (assert* (not (pcall get {})))) - (testing get + (testing get-in (local t {:a {:b {:c 10}}}) - (assert-eq (get t [:a]) {:b {:c 10}}) - (assert-eq (get t [:a :b]) {:c 10}) - (assert-eq (get t [:a :b :c]) 10) - (assert-eq (get t [:a :b :c] false) 10) - (assert-eq (get t [:a :b :d] false) false) - (assert-eq (get t [:a :b :d]) nil))) + (assert-eq (get-in t [:a]) {:b {:c 10}}) + (assert-eq (get-in t [:a :b]) {:c 10}) + (assert-eq (get-in t [:a :b :c]) 10) + (assert-eq (get-in t [:a :b :c] false) 10) + (assert-eq (get-in t [:a :b :d] false) false) + (assert-eq (get-in t [:a :b :d]) nil) + (assert-eq (get-in t []) t) + (assert* (not (pcall get-in))) + (assert* (not (pcall get-in {}))))) |