我有一个构建树的递归 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 如何处理递归和闭包范围有关。
请赐教:)