You're viewing version 1.2.0 of get. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16
  • (get map key)
  • (get map key not-found)
Returns the value mapped to key, not-found or nil if key not present.

5 Examples top

  • user=> (get [1 2 3] 1)
    2
    user=> (get [1 2 3] 5)
    nil
    user=> (get {:a 1 :b 2} :b)
    2
    user=> (get {:a 1 :b 2} :z "missing")
    "missing"
    
    
  • ;; to get an index of the element of a vector, use .indexOf
    user=> (def v ["one" "two" "three" "two"])
    #'user/v
    
    user=> (.indexOf v "two")
    1
    
    user=> (.indexOf v "foo")
    -1
    
  • user=> (get (System/getenv) "SHELL")
    "/bin/bash"
    
    user=> (get (System/getenv) "PATH")
    "/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/bin"
  • ;; 'get' is not the only option
    user=> (def my-map {:a 1 :b 2 :c 3})
    
    ;; maps act like functions
    user=> (my-map :a)
    1
    
    ;; even keys act like functions
    user=> (:b my-map)
    2
  • user=> (get '(a b c) 1)
    nil
    
    ;; use nth
    user=> (nth '(a b c) 1)
    b
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1154 top

(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))))
Vars in clojure.core/get: defn key map

Comments top

No comments for get. Log in to add a comment.