ツリーを構築する再帰的な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が再帰とクロージャのスコープを処理する方法と関係があるのではないかと思います。
私に教えてください:)