liveBook · Manning

Get method

As explained in Clojure: The Essential Reference

get is a Clojure function that retrieves a value associated with a key or index from a collection and returns nil or a supplied fallback when lookup is unavailable.

`get` looks up a key or index without throwing merely because it is absent or the collection is unsupported. Its forms are `(get coll key)` and `(get coll key not-found)`: an unsuccessful lookup returns `nil` by default, or the supplied `not-found` value. The function is primarily for maps, but its result depends on the collection type. section 8.2.2 section 8.2

Map lookup and a fallback

`(get {:a "a" :b "b"} :a)` returns `"a"`. `(get {:a "a"} :missing "Not found")` returns `"Not found"`. A fallback also handles an unsupported collection: `(get (list 1 2 3) 1 "Not found")` returns `"Not found"`. section 8.2.2

The same call has different meanings across supported inputs:

  • For maps, including records and `java.util.Map` implementations, it retrieves the value associated with a key. For a set, it returns the matching element. section 8.2.2
  • For a vector, string, or native Java array, a numeric key selects an indexed item. `nil` can be passed as the collection; it yields `nil` or the supplied fallback. section 8.2.2
  • A fallback does not prevent every exception: an incompatible key type can cause `ClassCastException` with a sorted map or sorted set. Very large numeric indices can be truncated when used with vectors, strings, or arrays. section 8.2.2
(def mixed-bag [{1 "a"} [0 2 4] nil "abba" 3 '(())])

(map #(get % 1) mixed-bag) ; #1
;; ("a" 2 nil \b nil nil)

A single lookup over a mixed collection yields a map value, vector item, string character, or `nil`, depending on each input. section 8.2.2

Choose the access operation by the result you need. `find` returns a map entry rather than just its value; `get-in` follows a path through nested collections. Maps and keywords can also be called for map lookup. Use `get` when a value—or a chosen fallback—is the useful result. section 8.2.2

How Clojure: The Essential Reference covers it

How deep it goes
Defines both arities, input-specific results, fallbacks, and notable exceptions, including sorted-key incompatibility and oversized indices.
Examples
Demonstrates map lookup, unsupported lists, mixed collections, sorted-map exceptions, and lookup through nested data.
What it assumes
Assumes familiarity with Clojure collection literals and function calls; defines `get`'s lookup contract rather than teaching those basics.
What you can do after
Apply it via Clojure's `get` to retrieve values or supply fallbacks while recognizing inputs that need another access function.
How it connects
Relates `get` to `nth`, `find`, `get-in`, callable maps, and callable keywords as collection-access alternatives.

Also explained in

Compare how 2 books explain get method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.