简体   繁体   English

在Clojure中遍历,迭代,访问树木

[英]Traversing, iterating, visiting trees in Clojure

Say I have a Clojure tree as defined below 假设我有一个如下定义的Clojure树

(def eval-logic
  '(:OR
     (:METHOD "methodName1" ["a1" "a2" "a3"])
     (:METHOD "methodName2" ["b1" "b2" "b3"])
     :AND
       (:METHOD "methodName3" [])
       (:METHOD "methodName4" ["d1"])
     ))

Ie the tree represents a boolean expression where nodes like :OR and :AND represent boolean operators and the leaves of the tree are method calls. 即树表示一个布尔表达式,其中节点如:OR和:AND表示布尔运算符,树的叶子是方法调用。 The tree is not to be evaluated, only to be traversed to generate code that performs the actual evaluation at runtime. 不评估树,仅遍历以生成在运行时执行实际评估的代码。 How should I go about traversing the tree, visiting different kinds of nodes, asking for a node's father, children, siblings, etc.? 我应该如何遍历树,访问不同类型的节点,询问节点的父亲,孩子,兄弟姐妹等? Are there any pointers or libraries to look into ? 是否有任何指针或库可供查看?

The tree cannot be too deeply nested so tail-recursion is not a concern. 树不能太嵌套,因此尾递归不是问题。 Feel free to propose some other data-structure that might perhaps be more amenable to traversing. 随意提出一些可能更适合遍历的其他数据结构。

I am particularly interested for depth-first traversals as this is the order that code will have to generated to ensure functions are defined before they are used. 我对深度优先遍历特别感兴趣,因为这是必须生成代码以确保在使用之前定义函数的顺序。 Also, the tree is given and won't change so I'm not interested in things like adding or removing children 此外,树是给定的,不会改变,所以我对添加或删除孩子等事情不感兴趣

To move around trees at will see clojure.zip . 随意移动树木,请参阅clojure.zip For simple walks, see clojure.walk and tree-seq . 对于简单的散步,请参阅clojure.walktree-seq There is an IBM article on the subject at Tree visitors in Clojure . 在Clojure的Tree访问者中有一篇关于这个主题的IBM文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM