如何以类似于线程的方式在clojure中导航地图

shm*_*111 1 clojure

有没有更好的方法来浏览clojure中的嵌套地图.例如,对于以下地图:

{
  :one{
    :two {
      :three value
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

为了获得:three我所做的价值,(:three (:two (:one mymap)))但是如果有我可以做的线程之类的话会更好(-> mymap :one :two :three)

tan*_*mer 6

核心功能get-in完全符合您的要求

Returns the value in a nested associative structure,
where ks is a sequence of ke(ys. Returns nil if the key is not present,
or the not-found value if supplied.


(get-in your-nested-data [:one :two :three])
Run Code Online (Sandbox Code Playgroud)

http://clojuredocs.org/clojure_core/clojure.core/get-in


A. *_*ebb 6

"如果有我可以做的线程,会更好(-> mymap :one :two :three)"

谁说你做不到?你的确切语法有效!

so.core=> (def mymap { :one, { :two, { :three :value } } })
#'so.core/mymap
so.core=> (-> mymap :one :two :three)
:value
Run Code Online (Sandbox Code Playgroud)

  • 在我问这个问题之前没有真正尝试过的道歉!至少其他人可能会觉得这个页面很有用,因为我找不到谷歌的任何内容,这就是我问的原因. (2认同)