liveBook · Manning

Find

As explained in Clojure: The Essential Reference

find is a Clojure function that searches an associative collection for a key and returns the corresponding key-value entry or nil.

find searches an associative collection for a key and returns a key-value entry when the key is present, or nil otherwise. It accepts Clojure maps, Java Map implementations, records, vectors, subvectors, and native vectors. The returned entry is a java.util.Map$Entry; use key and val to extract its components. section 11.2.2

Listing 11.7. → Searching, Key-value pair
(find [map key])
(key [e])
(val [e])

Use the compact signatures to compare find with key and val: find locates an entry, while the other functions extract its key or value. section 11.2.2

Map lookup returns an entry

(find {:a 1 :b 2} :b) ;; [:b 2] (type (find {:a 1 :b 2} :b)) ;; clojure.lang.MapEntry (key (find {:a 1 :b 2} :b)) ;; :b

Vector lookup uses an index

(find [:a :b :c] 1) ;; [1 :b] (find [:a :b :c] :c) ;; nil (find (subvec [:x :a :b :c] 1 3) 1) ;; [1 :b]

Choose find when the entry itself matters, especially when processing several maps:

  • Use get when you want only the value, an optional not-found value, or flexible access across supported collection types. section 8.2.2
  • Treat non-index vector keys cautiously; unusual numeric keys can produce unpredictable results rather than a meaningful lookup. section 11.2.2

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, supported collections, edge cases, and entry extraction, but does not develop implementation internals.
Examples
Demonstrates map entries, vector indexes, subvectors, missing keys, and filtering records with keep.
What it assumes
Assumes familiarity with Clojure collections and indexes, while defining associative lookup and Map.Entry extraction directly.
What you can do after
Apply it via Clojure to locate entries, extract keys or values, and retain matching records without exceptions for absent keys.
How it connects
Connects associative collections, vectors, map entries, key, val, keep, and get.

Also explained in

Compare how 3 books explain find →

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.