title: how do I build a tree hierarchy from a list in groovy using recursive closure?
body: <p>I defined the recursive domain class in grails:</p>
  
  <pre><code>class Work {
  
    String code
    String title
    String description
    static hasMany = [subWorks:Work]
    static mappedBy = [subWorks: 'parentWork']
  
    Work getRootWork(){
      if(parentWork) return parentWork.getRootWork()
        else return this
    }
  
    boolean isLeafWork(){
      return subWorks.isEmpty()
    }
  
    boolean isRootWork(){
      return !parentWork
    }
  </code></pre>
  
  <p>I have a list of Works, but the hierarchy structure is not built yet. The structure looks like:</p>
  
  <pre><code>def works = [new Work(code:'A', title:'TitleA'), 
      new Work(code:'B', title:'TitleB'), 
      new Work(code:'A.1', title:'Titile A.1'), 
      new Work(code:'B.1', title:'Title B.1'),
      new Work(code:'B.2', title:'Title B.2'),
      new Work(code:'B.3', title:'Title B.3'), 
      new Work(code:'B.2.2', title:'Title B.2.2'),
      new Work(code:'B.2.3', title:'Title B.2.3'),
      new Work(code:'A.1.1', title:'Title A.1.1'),
      new Work(code:'A.1.2', title:'Title A.1.2'),]
  </code></pre>
  
  <p>What I need is to build the hierarchical relationship among these works, based on the code hinted. e.g. A.1 is the first child work of A; B.1.1 is the first child of B.1 work, whose parent is B work. I know that Groovy supports recursive closures to build this kind of hierarchical structure. How do I achieve my goal using Groovy recursive closure, such as the JN2515 Fibonacci number example, in Groovy official documentation?
  Many thanks!</p>