Groovy如何处理闭包范围和递归?

Groovy如何处理闭包范围和递归?,groovy,recursion,jvm,closures,jvm-languages,Groovy,Recursion,Jvm,Closures,Jvm Languages,我有一个构建树的递归Python函数,我正在尝试将其转换为Groovy 这是Python版本 def get_tree(vertices): results = [] if type(vertices) != list: vertices = [vertices] for vertex in vertices: results.append(vertex) children = get_children(vertex)

我有一个构建树的递归Python函数,我正在尝试将其转换为Groovy

这是Python版本

def get_tree(vertices):
    results = []

    if type(vertices) != list:
        vertices = [vertices]

    for vertex in vertices:
        results.append(vertex)
        children = get_children(vertex)
        if children:
            child_tree = get_tree(children)
            results.append(child_tree)

    return results
下面是get_tree1的输出

  [1, [2, 3, 4, [5, 3]]]
这是我将其转换为Groovy闭包的尝试

_tree = { vertices ->

  results = []

  vertices.each() {
    results << it
    children = it."$direction"().toList()
    if (children) {
      child_tree = _tree(children)
      results << child_tree
    }
  }
  results
}
这些收藏品是关于什么的

我对Groovy只有粗略的了解,我怀疑这与Groovy如何处理递归和闭包作用域有关

请告诉我:

解决方案是将def添加到results=[]:


请参见

您没有提供足够的信息来创建一个工作示例。例如,您向该方法提供了哪些数据?您可能应该从一个更简单的示例开始。最初Python和Groovy方法都提供了一个数据库ID。在本例中,初始数据库ID是数字1。Python get_children方法和Groovy it.$direction.toList正在返回一个相对于父对象的id列表——在这个特定的例子中,它正在构建一个线程注释树。我认为@OverZealous的意思是,示例代码中存在太多的歧义,无法提供帮助。您需要将其简化为一个可以以独立方式执行的简明示例。这至少意味着所有涉及的方法都有静态示例数据和示例代码。
gremlin> g.v(1).outTree()    
==>[v[5], v[3], (this Collection), (this Collection)]
_tree = { vertices ->

  def results = []

  vertices.each() {
    results << it
    children = it."$direction"().toList()
    if (children) {
      child_tree = _tree(children)
      results << child_tree
    }
  }
  results
}