store/[org.clojure/clojure "1.6.0"] clj::clojure.core/get

Community Documentation

Edit

get works for several types of arg 'map', not only maps:

  • maps, including records and Java objects implementing java.util.Map
  • sets, but not Java objects implementing java.util.Set
  • vectors, where the key is the index of the element to get
  • strings and Java arrays, where again the key is the index
  • nil, for which get always returns not-found or nil

If you want a simpler way to determine whether a key is in a map or an element is in a set, without having to know a not-found value that is guaranteed not to be a return value for a key/element in the collection, use clj::clojure.core/contains?

The only conditions in which get will throw an exception is indirectly, e.g. because you called it on a sorted set or map, and the comparator function throws an exception when comparing two values. It will not even throw an exception if you use an out-of-bounds index for a vector or array.

See also:

Source

(defn get
  "Returns the value mapped to key, not-found or nil if key not present."
  {:inline (fn  [m k & nf] `(. clojure.lang.RT (get ~m ~k ~@nf)))
   :inline-arities #{2 3}
   :added "1.0"}
  ([map key]
   (. clojure.lang.RT (get map key)))
  ([map key not-found]
   (. clojure.lang.RT (get map key not-found))))

Example 1

Edit
;; The symbol b is in the set, and a set is an identity mapping.
(get #{"a" 'b 5 nil} 'b)
;; => b

;; 2 is not

(get #{"a" 'b 5 nil} 2)
;; => nil

;; nil is in this set, but one cannot distinguish this result from not
;; being in the set.

(get #{"a" 'b 5 nil} nil)
;; => nil

;; The same is true here.

(get #{"a" 'b 5} nil)
;; => nil

;; You may specify a not-found value to help distinguish these cases.
;; This works well as long as there is some value you know is not in the
;; set.

(get #{"a" 'b 5} nil :not-found)
;; => :not-found

(get #{"a" 'b 5 nil} nil :not-found)
;; => nil

;; Similarly for maps:
(get {"a" 1, "b" nil} "b")
;; => nil
;; found key "b", but value is nil

(get {"a" 1, "b" nil} "b" :not-found)
;; => nil
;; here we can tell it was found and value is nil

(get {"a" 1, "b" nil} "c" :not-found)
;; => :not-found
;; but here no key "c" was found

Example 2

Edit
;; 'get' is not the only option
(def my-map {:a 1 :b 2 :c 3})
;; => #'user/my-map

;; maps act like functions
(my-map :a)
;; => 1

;; even keys act like functions
(:b my-map)
;; => 2

Example 3

Edit
(get '(a b c) 1)
;; => nil

;; use nth
(nth '(a b c) 1)
;; => b

Example 4

Edit
;; to get an index of the element of a vector, use .indexOf
(def v ["one" "two" "three" "two"])
;; => #'user/v

(.indexOf v "two")
;; => 1

(.indexOf v "foo")
;; => -1

Example 5

Edit
(get (System/getenv) "SHELL")
;; => "/bin/bash"

(get (System/getenv) "PATH")
;; => "/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/bin"

Example 6

Edit
(get [1 2 3] 1)
;; => 2

(get [1 2 3] 5)
;; => nil

(get {:a 1 :b 2} :b)
;; => 2

(get {:a 1 :b 2} :z "missing")
;; => "missing"

Example 7

Edit
;; Also similar to contains?, 'get' indices are truncated to 32-bit ints,
;; so some large integers that are out of index bounds for a vector,
;; string, or array can be truncated to 32-bit ints that are in range
;; after removing their most significant bits.

(def long-truncates-to-int-0 (bit-shift-left 1 33))
;; => #'user/long-truncates-to-int-0

(get "abc" long-truncates-to-int-0)
;; => \a

(get [:a :b :c] long-truncates-to-int-0)
;; => :a

Example 8

Edit
;; Clojure allows you to leave out the 'get' to achieve a more concise
;; syntax for vectors and maps.  Differences:
;; 
;; * No not-found value vectors.  You may use one for maps.
;; * (my-vector idx) will throw an exception if idx is not an integer, or
;;   out of the bounds for the vector.
;; * It does not work for records, strings, or Java Maps or arrays.
;; 

(def vec1 [:a :b :c])
;; => #'user/vec1

(vec1 2)
;; => :c

(vec1 3)
;; => IndexOutOfBoundsException   clojure.lang.PersistentVector.arrayFor (PersistentVector.java:107)

(vec1 3 :not-found)
;; => ArityException Wrong number of args (2) passed to: PersistentVector  clojure.lang.AFn.throwArity (AFn.java:437)

(def map1 {:a 1 :b 2})
;; => #'user/map1

(map1 :a)
;; => 1

(map1 :c)
;; => nil

(map1 :c :not-found)
;; => :not-found

Example 9

Edit
;; Similar to contains?, `get` has some unusual cases where non-integer
;; numbers will be rounded off to integers without any errors.

(get [:a :b :c] 1.7)
;; => nil ; on a vector, not found

(get (int-array [5 6 7]) -0.99)
;; => 5   ; on a Java array, truncate to int, then index is found

Uses on crossclj