Clojure - 步行与路径 [英] Clojure - walk with path
本文介绍了Clojure - 步行与路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在寻找类似 clojure.walk 中具有 inner
函数接受参数:
I am looking for a function similar to those in clojure.walk that have an inner
function that takes as argument :
- 不是键和值,具有clojure.walk / walk功能的情况
- 但是从顶层数据结构访问值所需的键向量。
- 递归遍历所有数据
示例:
;; not good since it takes `[k v]` as argument instead of `[path v]`, and is not recursive.
user=> (clojure.walk/walk (fn [[k v]] [k (* 10 v)]) identity {:a 1 :b {:c 2}})
;; {:a 10, :c 30, :b 20}
;; it should receive as arguments instead :
[[:a] 1]
[[:b :c] 2]
注意:
- 键0,1,2 ...(就像
get-in
)。 -
outer
参数,如果允许简化代码。
- It should work with arrays too, using the keys 0, 1, 2... (just like in
get-in
). - I don't really care about the
outer
parameter, if that allows to simplify the code.
推荐答案
目前学习clojure,我尝试这是一个练习。
然而,我发现很难实现它直接作为一个应用内部函数的树的步骤。
Currently learning clojure, I tried this as an exercise. I however found it quite tricky to implement it directly as a walk down the tree that applies the inner function as it goes.
为了实现结果,寻找,我在2中拆分任务:
To achieve the result you are looking for, I split the task in 2:
- 首先将嵌套结构转换为字典,路径为键,
- 然后将内部函数映射到外部函数,或减少外部函数。
:
;; Helper function to have vector's indexes work like for get-in
(defn- to-indexed-seqs [coll]
(if (map? coll)
coll
(map vector (range) coll)))
;; Flattening the tree to a dict of (path, value) pairs that I can map over
;; user> (flatten-path [] {:a {:k1 1 :k2 2} :b [1 2 3]})
;; {[:a :k1] 1, [:a :k2] 2, [:b 0] 1, [:b 1] 2, [:b 2] 3}
(defn- flatten-path [path step]
(if (coll? step)
(->> step
to-indexed-seqs
(map (fn [[k v]] (flatten-path (conj path k) v)))
(into {}))
[path step]))
;; Some final glue
(defn path-walk [f coll]
(->> coll
(flatten-path [])
(map #(apply f %))))
;; user> (println (clojure.string/join "\n" (path-walk #(str %1 " - " %2) {:a {:k1 1 :k2 2} :b [1 2 3]})))
;; [:a :k1] - 1
;; [:a :k2] - 2
;; [:b 0] - 1
;; [:b 1] - 2
;; [:b 2] - 3
这篇关于Clojure - 步行与路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文