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

发布于 2024-11-30 16:19:48 字数 1044 浏览 1 评论 0原文

我有一个递归 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_tree(1) 的输出...

  [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
}

但这不起作用 - 这就是它返回的...

gremlin> g.v(1).outTree()    
==>[v[5], v[3], (this Collection), (this Collection)]

什么是那些“这个集合”是关于什么的?

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

请赐教:)

I have a recursive Python function that builds a tree, and I'm trying to translate it into Groovy.

Here's the Python version...

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

Here's the output of get_tree(1)...

  [1, [2, 3, 4, [5, 3]]]

And here's my attempt to translate this into a Groovy closure...

_tree = { vertices ->

  results = []

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

But this doesn't work -- this is what it returns...

gremlin> g.v(1).outTree()    
==>[v[5], v[3], (this Collection), (this Collection)]

What are those "this Collection"s about?

I have only a cursory understanding of Groovy, and I suspect it's something to do with how Groovy handles recursion and closure scope.

Please enlighten me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-12-07 16:19:48

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

_tree = { vertices ->

  def results = []

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

请参阅 https://groups.google.com/d/msg/gremlin-users/iCPUifiU_wk/mrUhsjOM2h0J

The solution was to add def to results = []:

_tree = { vertices ->

  def results = []

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

See https://groups.google.com/d/msg/gremlin-users/iCPUifiU_wk/mrUhsjOM2h0J

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文