Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pixie/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1061,17 +1061,22 @@ If further arguments are passed, invokes the method named by symbol, passing the

(extend -repr Symbol -str)

(extend -invoke Keyword (fn [k m] (-val-at m k nil)))
(extend -invoke PersistentHashMap (fn [m k] (-val-at m k nil)))
(extend -invoke PersistentHashSet (fn [m k] (-val-at m k nil)))

(defn get
{:doc "Get an element from a collection implementing ILookup, return nil or the default value if not found."
:added "0.1"}
([mp k]
(get mp k nil))
([mp k not-found]
(-val-at mp k not-found)))
(-val-at mp k not-found)))

(extend -invoke Keyword (fn
([k m not-found]
(-val-at m k not-found))
([k m]
(-val-at m k nil))))
(extend -invoke PersistentHashMap get)
(extend -invoke PersistentHashSet get)


(defn get-in
{:doc "Get a value from a nested collection at the \"path\" given by the keys."
Expand Down
5 changes: 3 additions & 2 deletions tests/pixie/tests/collections/test-maps.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
(t/assert= (-eq m {:a 1, :b 2, :c 4}) false)
(t/assert= (-eq m {:a 3, :b 2, :c 1}) false)))


(t/deftest map-val-at-and-invoke
(let [m {:a 1, :b 2, :c 3}]
(foreach [e m]
(t/assert= (get m (key e)) (val e))
(t/assert= (m (key e)) (val e)))
(t/assert= (get m :d) nil)
(t/assert= (m :d) nil)))
(t/assert= (m :d) nil)
(t/assert= (m :d :foo) :foo)
(t/assert= (:d m :foo) :foo)))

(t/deftest map-without
(let [m {:a 1 :b 2}]
Expand Down
4 changes: 3 additions & 1 deletion tests/pixie/tests/collections/test-sets.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
(t/assert= (s 3) 3)

(t/assert= (s -1) nil)
(t/assert= (s 4) nil)))
(t/assert= (s 4) nil)
(t/assert= (s :d :foo) :foo)
(t/assert= (:d s :foo) :foo)))

(t/deftest test-has-meta
(let [m {:has-meta true}
Expand Down
3 changes: 2 additions & 1 deletion tests/pixie/tests/test-keywords.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
(t/assert= (:b m) 2)
(t/assert= (:c m) 3)

(t/assert= (:d m) nil)))
(t/assert= (:d m) nil)
(t/assert= (:d m :foo) :foo)))

(t/deftest keyword-namespace
(t/assert= (namespace :foo/bar) "foo")
Expand Down