2
votes

Which of these is more idiomatic Clojure?

(def book {:title "Joy of Clojure" 
           :authors ["Michael Fogus" "Chris Houser"]})

(get-in book [:authors 0])
;; => "Michael Fogus"

(-> book :authors first)
;; => "Michael Fogus"

When I have much more complicated data structures, this becomes more relevant. Presumably there's no technical difference between the two?

1

1 Answers

6
votes

get-in is better for nested structures, because many interesting keys are not callable, in particular indexes in a vector (other than first or second) or string keys in hash-maps.

user=> (get-in [{:a 0} 1 nil "unknown" {:b {"context info" 42}}] [4 :b "context info"])
42