Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8784245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:02:16+00:00 2026-06-13T21:02:16+00:00

In our current application we have a need to traverse down a tree and

  • 0

In our current application we have a need to traverse down a tree and capture all operators on a specific device (and child devices). A device could have child devices with also specific operators on it.

As i am new to the use of recursion in Groovy i am wondering if i am doing things right..?
Any pointer to help me learn better ways of doing things?

def listOperators(device) {
    // list with all operator id's
    def results = []

    // closure to traverse down the tree
    def getAllOperators = { aDevice->
        if(aDevice) {
            aDevice.operators.each { it ->
                results << it.id
            }
        }
        if (aDevice?.children) {
            aDevice.children.each { child ->
                results << owner.call(child)
            }
        }
    }

    // call the closure with the given device
    getAllOperators(device)

    // return list with unique results
    return results.unique()
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:02:17+00:00Added an answer on June 13, 2026 at 9:02 pm

    A couple things to note:

    • Doing the recursive call through owner is not a good idea. The definition of owner changes if the call is nested within another closure. It’s error prone and has no advantages over just using the name. When the closure is a local variable, split its up the declaration and definition of the closure so the name is in scope. E.g.:

      def getAllOperators
      getAllOperators = { ...

    • You are appending the operators to a result list outside the recursive closure. But you are also appending the result of each recursive call to the same list. Either append to the list or store the results from each recursive call, but not both.

    Here’s a simpler alternative:

    def listOperators(device) {
        def results = []
        if (device) {
            results += device.operators*.id
            device.children?.each { child ->
                results += listOperators(child)
            }
        }
        results.unique()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a WPF application that I need to authenticate with our current
I'm working on an ASP.NET project where I need to split our current application
I have a situation where I need to replace our current file based cache
A common scenerio we are running into with our current application is where we
Our current product is based on Eclipse RCP. We are starting to have problems
Our current MVC project is set up to have ViewModels that encapsulate the data
For our current J2EE project based on JBoss, we need to interface with a
I have run into an interesting problem with the current application I'm working on
In our current project, we need some high level DBI for different databases. It
We have an Internet facing Web Application running, and recently our company made some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.