Returns the value mapped to key, not-found or nil if key not present in associative collection, set, string, array, or ILookup instance.
(get [1 2 3] 1) ;;=> 2 (get [1 2 3] 5) ;;=> nil (get [1 2 3] 5 100) ;;=> 100 (get {:a 1 :b 2} :b) ;;=> 2 (get {:a 1 :b 2} :z "missing") ;;=> "missing"
;; 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
;; the system environment has a hash-map semantic (get (System/getenv) "SHELL") ;;=> "/bin/bash" (get (System/getenv) "PATH") ;;=> "/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/bin"
;; 'get' is not the only option (def my-map {:a 1 :b 2 :c 3}) ;; maps act like functions taking keys (my-map :a) ;;=> 1 ;; even keys (if they are keywords) act like functions (:b my-map) ;;=> 2
;; it is tempting to try an index on a list (get '(a b c) 1) ;;=> nil ;; but you should use nth (nth '(a b c) 1) ;;=> b
;; For sorted stuff, "key" must be of the same type of the existing keys. ;; This allows descending the sorted tree in log(N) average. (get (hash-map :a 1 :b 2) "a" "not found") ;; "not found" (get (sorted-map :a 1 :b 2) "a" "not found") ;; ClassCastException ;; get works on transient maps, but silently fails on transient sets. ;; A similar issue affects contains?. (get (transient #{0 1 2}) 1) ;; nil ;; get uses int cast with precision loss, with (.intValue x). ;; The example below is explained because 4294967296 equal 2^32, thus ;; (.intValue 4294967296) returns 0. ;; Be careful with sufficiently large keys: (get ["a" "b" "c"] 4294967296) ;; "a"
;; Specifying 'not-found' does NOT guarantee that the return value is ;; not 'nil'. (get {} :a 42) ;;=> 42 (get {:a nil} :a 42) ;;=> nil ;; This may be especially relevant if you use records, as all pre-defined keys ;; will usually be present. (defrecord R [a b]) (def r (map->R {})) r ;;=> #user.R{:a nil :b nil} (get r :a 42) ;;=> nil ;; Consider using 'or' instead, as 'nil' is "falsy". (or (get {:a nil} :a) 42) ;;=> 42
;; Specifying not-found doesn't mean that the expression won't be evaluated. ;; This can be a problem if you intend to materialize the not-found value ;; only if key is not set. (get {:a 1} :a (#(println "else"))) ;; else ;;=> 1 ;; using an `or` can solve your problem here (or (get {:a 1} :a) (println "not found")) ;;=> 1
;; how to deal with nil map, defensive coding example: ;; try to get :a key from nil map (get nil :a :a1) ;;=> :a1
Returns a lazy sequence consisting of the result of applying f to 0 and the first item of coll, fo...
Returns the value in a nested associative structure, where ks is a sequence of keys. Returns nil i...
Returns a map containing only those entries in map whose key is in keys
Returns the value at the index. get returns nil if index out of bounds, nth throws an exception un...
(:mykey my-hash-map :none) means the same as (get my-hash-map :mykey :none) and
('mysym my-hash-map :none) means the same as (get my-hash-map 'mysym :none).
So, you can use (:a {:a 1 :b 2} :not-inserted) as (get {:a 1 :b 2} :a :not-inserted).
See
http://clojure.org/data_structures#Data Structures-Keywords and
http://clojure.org/data_structures#Data Structures-Symbols