1

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

私に教えてください:)

4

1 に答える 1

0

解決策は次のように追加することでしdefresults = []

_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を参照してください

于 2011-09-07T01:45:54.637 に答える