Clojure - 沿着路径行走 [英] Clojure - walk with path

查看:17
本文介绍了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 "
" (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆