Reputation: 2358
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?
Upvotes: 2
Views: 212
Reputation: 20194
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
Upvotes: 6