从 Clojure 中的嵌套结构获取的惯用方法

Idiomatic way to get from nested structures in Clojure

以下哪个 Clojure 更地道?

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

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

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

当我有更复杂的数据结构时,这就变得更重要了。想必两者在技术上没有区别吧?

get-in 更适合嵌套结构,因为许多有趣的键是不可调用的,特别是向量中的索引(firstsecond 除外)或哈希中的字符串键-地图。

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